// ManualMode.cpp : implementation file
//

#include "stdafx.h"
#include "voldia.h"
#include "ManualMode.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define TIMER_FAST_ID			1
#define TIMER_SLOW_ID			2
#define	TIMER_FAST_PERIOD		500
#define	TIMER_SLOW_PERIOD		2500

#define PROG_STEPS				200

#define MOV_NULL				0
#define	MOV_BRIDGE				1
#define	MOV_TROLLEY				2
#define	MOV_MM					4
#define	MOV_MM_FIX				5
#define	MOV_MM_CLUS				6
#define	MOV_MM_ROT				7
#define	MOV_TV					8
#define	MOV_TV_ROT				9
#define MOV_FALV				10
#define MOV_MM_DISPL			11

#define	SP_OK					0
#define	SP_OUT_OF_BOUND			1
#define SP_IN_POS				2

#define CMB_MM_FIX_OP			0
#define CMB_MM_FIX_CL			1

#define CMB_MM_DISPL_DOWN		0
#define CMB_MM_DISPL_UP			1

#define CMB_MM_FALVL_DOWN		0
#define CMB_MM_FALVL_UP			1

#define MSG_OK					0
#define MSG_WRN					1
#define MSG_ERR					2

#define UD_TOP_FALSE			0
#define UD_DOWN_FALSE			1
#define UD_LISTS_FALSE			2
#define UD_ECHO_FALSE			3
#define UD_TOP_TRUE				4
#define UD_DOWN_TRUE			5
#define UD_LISTS_TRUE			6
#define UD_ECHO_TRUE			7

#define END_MOV_SLEEP			1000

#define MM_SP_LOWER_TOLL		20.0

#define NP_WAIT					250
#define NP_MAX_COUNT			1

#define SP_CHECK_CYCLES			30
#define	SP_CHECK_WAIT			250
#define	SP_CHECK_INIT_WAIT		500

#define MAN_SLEEP_AFTER_START	1000

extern CVolDiaApp theApp;
/////////////////////////////////////////////////////////////////////////////
// ManualMode dialog


ManualMode::ManualMode(CWnd* pParent /*=NULL*/)
	: CDialog(ManualMode::IDD, pParent)
{
	//{{AFX_DATA_INIT(ManualMode)
	m_cellInfo = _T("");
	m_txtUser = _T("");
	m_txtZone = _T("");
	m_txtVirtCoo = _T("");
	m_txtOpMode = _T("");
	m_MM_fix_pos = _T("");
	m_txtDescr = _T("");
	m_txtStatus = _T("");
	m_txtDate = _T("");
	m_MMFixSp = _T("");
	m_Bridge_v = _T("");
	m_MMClus_v = _T("");
	m_MMFix_v = _T("");
	m_MM_v = _T("");
	m_Trolley_v = _T("");
	m_TV_v = _T("");
	m_TVR_dir = _T("");
	m_wgCl_act = _T("");
	m_wgCl_exp = _T("");
	m_wgMM_act = _T("");
	m_wgMM_exp = _T("");
	m_sysStatus = _T("");
	m_displ_A = _T("");
	m_displ_B = _T("");
	m_MMDisp_pos = _T("");
	m_MMDisp_dir = _T("");
	m_MMDisp_sp = _T("");
	m_FalvPos = _T("");
	m_FALVL_sp = _T("");
	//}}AFX_DATA_INIT

	this->m_curNormal = ::LoadCursor(0, IDC_ARROW);
	this->m_curHour = ::LoadCursor(0, IDC_WAIT);
}


void ManualMode::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(ManualMode)
	DDX_Control(pDX, IDC_MNT_DIMP, m_bMntDimp);
	DDX_Control(pDX, IDC_MNT_FALV, m_bMntFALV);
	DDX_Control(pDX, IDC_MNT_TVM, m_bMmtTVM);
	DDX_Control(pDX, IDC_MNT_MM, m_bMntMM);
	DDX_Control(pDX, IDC_CMB_FALV, m_cmbFALVL_sp);
	DDX_Control(pDX, IDC_DIMP_MTN_RECT, m_DIMPMtnRect);
	DDX_Control(pDX, IDC_MM_MTN_RECT, m_MMMtnRect);
	DDX_Control(pDX, IDC_TVM_MNT_RECT, m_TVMtnRect);
	DDX_Control(pDX, IDC_FALV_MTN_RECT, m_FALVMtnRect);
	DDX_Control(pDX, IDC_DISPL_SEL, m_btnMMDispl);
	DDX_Control(pDX, IDC_TV_ROT_SEL, m_btnTVRot);
	DDX_Control(pDX, IDC_MM_CLUS_SEL, m_btnMMClus);
	DDX_Control(pDX, IDC_TV_SEL, m_btnTV);
	DDX_Control(pDX, IDC_TROLLEY_SEL, m_btnTrolley);
	DDX_Control(pDX, IDC_MM_SEL, m_btnMM);
	DDX_Control(pDX, IDC_MM_ROT_SEL, m_btnMMRot);
	DDX_Control(pDX, IDC_MM_FIX_SEL, m_btnMMFix);
	DDX_Control(pDX, IDC_FALVL_SEL, m_btnFALVL);
	DDX_Control(pDX, IDC_BRIDGE_SEL, m_btnBridge);
	DDX_Control(pDX, IDC_FALV_RECT, m_rectFalv);
	DDX_Control(pDX, IDC_MM_DISPL_RECT, m_rectDispl);
	DDX_Control(pDX, IDC_TV_ROT_RECT, m_rectTvRot);
	DDX_Control(pDX, IDC_MM_CL_RECT, m_rectClus);
	DDX_Control(pDX, IDC_MM_FIX_RECT, m_rectFAGRL);
	DDX_Control(pDX, IDC_MM_RECT, m_rectFAGR);
	DDX_Control(pDX, IDC_TV_RECT, m_rectTV);
	DDX_Control(pDX, IDC_TROLLEY_RECT, m_rectTrolley);
	DDX_Control(pDX, IDC_MM_ROT_RECT, m_rectMMRot);
	DDX_Control(pDX, IDC_BRIDGE_RECT, m_rectBridge);
	DDX_Control(pDX, IDC_CMB_DISPL, m_cmbMMDispl);
	DDX_Control(pDX, IDC_SYS_TIME, m_sysTime);
	DDX_Control(pDX, IDC_MM_CLUS_LL, m_MMClus_LL_c);
	DDX_Control(pDX, IDC_MM_CLUS_HL, m_MMClus_HL_c);
	DDX_Control(pDX, IDC_BTN_DBOP, m_btnDBOp);
	DDX_Control(pDX, IDC_RESET_TRIP, m_btnResetTrip);
	DDX_Control(pDX, IDC_MM_SP, m_MM_sp_rc);
	DDX_Control(pDX, IDC_MM_ANGLE_SP, m_MMR_sp_rc);
	DDX_Control(pDX, IDC_MM_CLUS_SP, m_Clus_sp_rc);
	DDX_Control(pDX, IDC_TV_SP, m_TV_sp_rc);
	DDX_Control(pDX, IDC_TV_ANGLE_SP, m_TVR_sp_rc);
	DDX_Control(pDX, IDC_SP_TROLLEY, m_Trolley_sp_rc);
	DDX_Control(pDX, IDC_SP_BRIDGE, m_Bridge_sp_rc);
	DDX_Control(pDX, IDC_DESCR_TXT, m_txtDescr_c);
	DDX_Control(pDX, IDC_ZONE_TXT, m_txtZone_c);
	DDX_Control(pDX, IDC_VIRT_COO_TXT, m_txtVirtCoo_c);
	DDX_Control(pDX, IDC_USER_TXT, m_txtUser_c);
	DDX_Control(pDX, IDC_TXT_DATE, m_txtDate_c);
	DDX_Control(pDX, IDC_TVR_LL, m_TVR_LL_c);
	DDX_Control(pDX, IDC_TVR_HL, m_TVR_HL_c);
	DDX_Control(pDX, IDC_TV_POS_LL, m_TV_pos_LL_c);
	DDX_Control(pDX, IDC_TV_POS_HL, m_TV_pos_HL_c);
	DDX_Control(pDX, IDC_TV_POS, m_TV_pos_c);
	DDX_Control(pDX, IDC_TV_ANGLE, m_TVR_pos_c);
	DDX_Control(pDX, IDC_STATUS_TXT, m_txtStatus_c);
	DDX_Control(pDX, IDC_POS_TROLLEY, m_Trolley_pos_c);
	DDX_Control(pDX, IDC_POS_BRIDGE, m_Bridge_pos_c);
	DDX_Control(pDX, IDC_OP_MODE_TXT, m_txtOpMode_c);
	DDX_Control(pDX, IDC_MMR_LL, m_MMR_LL_c);
	DDX_Control(pDX, IDC_MMR_HL, m_MMR_HL_c);
	DDX_Control(pDX, IDC_MM_POS, m_MM_pos_c);
	DDX_Control(pDX, IDC_MM_LL, m_MM_pos_LL_c);
	DDX_Control(pDX, IDC_MM_HL, m_MM_pos_HL_c);
	DDX_Control(pDX, IDC_MM_FIX_SP, m_MMFixSp_c);
	DDX_Control(pDX, IDC_MM_FIX_POS, m_MM_fix_pos_c);
	DDX_Control(pDX, IDC_MM_CLUS_POS, m_Clus_pos_c);
	DDX_Control(pDX, IDC_MM_ANGLE, m_MMR_pos_c);
	DDX_Control(pDX, IDC_LL_TROLLEY, m_Trolley_pos_LL_c);
	DDX_Control(pDX, IDC_LL_BRIDGE, m_Bridge_pos_LL_c);
	DDX_Control(pDX, IDC_HL_TROLLEY, m_Trolley_pos_HL_c);
	DDX_Control(pDX, IDC_HL_BRIDGE, m_Bridge_pos_HL_c);
	DDX_Control(pDX, IDC_CELL_INFO, m_cellInfo_c);
	DDX_Control(pDX, IDC_TV_SP_IN, m_TV_sp_c);
	DDX_Control(pDX, IDC_TV_ANGLE_SP_IN, m_TVR_sp_c);
	DDX_Control(pDX, IDC_SP_TROLLEY_IN, m_Trolley_sp_c);
	DDX_Control(pDX, IDC_SP_BRIDGE_IN, m_Bridge_sp_c);
	DDX_Control(pDX, IDC_MM_SP_IN, m_MM_sp_c);
	DDX_Control(pDX, IDC_MM_CLUS_SP_IN, m_Clus_sp_c);
	DDX_Control(pDX, IDC_MM_ANGLE_SP_IN, m_MMR_sp_c);
	DDX_Control(pDX, IDC_MSG_ICON, m_msgIcon);
	DDX_Control(pDX, IDC_LST_MAST, m_lstMast);
	DDX_Control(pDX, IDC_LST_CELL, m_lstCell);
	DDX_Control(pDX, IDC_CMB_FIX_SP, m_cmb_MMFix_sp);
	DDX_Control(pDX, IDC_PROG_MOV, m_progMov);
	DDX_Control(pDX, IDC_STOP, m_btnStop);
	DDX_Control(pDX, IDC_EXIT, m_btnExit);
	DDX_Control(pDX, IDC_EXE, m_btnExe);
	DDX_Text(pDX, IDC_CELL_INFO, m_cellInfo);
	DDX_Text(pDX, IDC_USER_TXT, m_txtUser);
	DDX_Text(pDX, IDC_ZONE_TXT, m_txtZone);
	DDX_Text(pDX, IDC_VIRT_COO_TXT, m_txtVirtCoo);
	DDX_Text(pDX, IDC_OP_MODE_TXT, m_txtOpMode);
	DDX_Text(pDX, IDC_MM_FIX_POS, m_MM_fix_pos);
	DDX_Text(pDX, IDC_DESCR_TXT, m_txtDescr);
	DDX_Text(pDX, IDC_STATUS_TXT, m_txtStatus);
	DDX_Text(pDX, IDC_TXT_DATE, m_txtDate);
	DDX_Text(pDX, IDC_MM_FIX_SP, m_MMFixSp);
	DDX_Text(pDX, IDC_BRIDGE_V, m_Bridge_v);
	DDX_Text(pDX, IDC_MM_CLUS_V, m_MMClus_v);
	DDX_Text(pDX, IDC_MM_FIX_V, m_MMFix_v);
	DDX_Text(pDX, IDC_MM_V, m_MM_v);
	DDX_Text(pDX, IDC_TROLLEY_V, m_Trolley_v);
	DDX_Text(pDX, IDC_TV_V, m_TV_v);
	DDX_Text(pDX, IDC_TVR_DIR, m_TVR_dir);
	DDX_Text(pDX, IDC_CL_ACT, m_wgCl_act);
	DDX_Text(pDX, IDC_CL_EXP, m_wgCl_exp);
	DDX_Text(pDX, IDC_MM_ACT, m_wgMM_act);
	DDX_Text(pDX, IDC_MM_EXP, m_wgMM_exp);
	DDX_Text(pDX, IDC_SYS_STATUS, m_sysStatus);
	DDX_Text(pDX, IDC_DISPL_A, m_displ_A);
	DDX_Text(pDX, IDC_DISPL_B, m_displ_B);
	DDX_Text(pDX, IDC_MM_DISPL_POS, m_MMDisp_pos);
	DDX_Text(pDX, IDC_MM_DISP_DIR, m_MMDisp_dir);
	DDX_Text(pDX, IDC_MM_DISPL_SP, m_MMDisp_sp);
	DDX_Text(pDX, IDC_FALV_POS, m_FalvPos);
	DDX_Text(pDX, IDC_FALVL_SP, m_FALVL_sp);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(ManualMode, CDialog)
	//{{AFX_MSG_MAP(ManualMode)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_EXE, OnExe)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_ACK, OnAck)
	ON_BN_CLICKED(IDC_RESET_TRIP, OnResetTrip)
	ON_BN_CLICKED(IDC_BTN_DBOP, OnBtnDbop)
	ON_WM_CLOSE()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_DUMMY, OnDummy)
	ON_CBN_SELCHANGE(IDC_CMB_FIX_SP, OnSelchangeCmbFixSp)
	ON_CBN_SELCHANGE(IDC_CMB_DISPL, OnSelchangeCmbDispl)
	ON_BN_CLICKED(IDC_BTN_REFRESH, OnBtnRefresh)
	ON_BN_CLICKED(IDC_BRIDGE_SEL, OnBridgeSel)
	ON_BN_CLICKED(IDC_TROLLEY_SEL, OnTrolleySel)
	ON_BN_CLICKED(IDC_MM_ROT_SEL, OnMmRotSel)
	ON_BN_CLICKED(IDC_MM_SEL, OnMmSel)
	ON_BN_CLICKED(IDC_MM_FIX_SEL, OnMmFixSel)
	ON_BN_CLICKED(IDC_MM_CLUS_SEL, OnMmClusSel)
	ON_BN_CLICKED(IDC_TV_ROT_SEL, OnTvRotSel)
	ON_BN_CLICKED(IDC_TV_SEL, OnTvSel)
	ON_BN_CLICKED(IDC_DISPL_SEL, OnDisplSel)
	ON_BN_CLICKED(IDC_FALVL_SEL, OnFalvlSel)
	ON_BN_CLICKED(IDC_MNT_MM, OnMntMm)
	ON_BN_CLICKED(IDC_MNT_TVM, OnMntTvm)
	ON_BN_CLICKED(IDC_MNT_FALV, OnMntFalv)
	ON_BN_CLICKED(IDC_MNT_DIMP, OnMntDimp)
	ON_CBN_SELCHANGE(IDC_CMB_FALV, OnSelchangeCmbFalv)
	ON_WM_SETFOCUS()
	//}}AFX_MSG_MAP
	ON_MESSAGE( WM_CHNG_MAST, OnMastChanged )
	ON_MESSAGE( WM_CHNG_CELL, OnCellChanged )
	ON_MESSAGE( WM_CHNG_SP, OnSpChanged )
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// ManualMode message handlers

BOOL ManualMode::OnInitDialog(){
	UINT			ret;
	BOOL			retB;
	RETCOND			retCnd;
	RETSTAT			retSt;
	TAGVALEX		tagVal;
	CDialog::OnInitDialog();
	
	retSt = GetLoggedUserLevel(theApp.m_pDlg->m_opStation, &this->m_loggedUser);
	if(retSt != RT_NORMAL)
		return(FALSE);

	this->m_cmb_MMFix_sp.ResetContent();
	this->m_cmb_MMFix_sp.InsertString(CMB_MM_FIX_OP, _T("OPEN"));
	this->m_cmb_MMFix_sp.InsertString(CMB_MM_FIX_CL, _T("CLOSE"));
	this->m_cmb_MMFix_sp.SetCurSel(0);

	this->m_cmbMMDispl.ResetContent();
	this->m_cmbMMDispl.InsertString(CMB_MM_DISPL_DOWN, _T("DOWN"));
	this->m_cmbMMDispl.InsertString(CMB_MM_DISPL_UP, _T("UP"));
	this->m_cmbMMDispl.SetCurSel(0);

	this->m_cmbFALVL_sp.ResetContent();
	this->m_cmbFALVL_sp.InsertString(CMB_MM_FALVL_DOWN, _T("DOWN"));
	this->m_cmbFALVL_sp.InsertString(CMB_MM_FALVL_UP, _T("UP"));
	this->m_cmbFALVL_sp.SetCurSel(0);

	retB = this->m_bmpGray.LoadBitmap(IDB_BMP_GRAY);
	retB = this->m_bmpGreen.LoadBitmap(IDB_BMP_GREEN);
	retB = this->m_bmpRed.LoadBitmap(IDB_BMP_RED);

	retCnd = TagRead(&theApp.m_pDlg->m_tiStart, &tagVal);
	if(retCnd != R_NORMAL)
		return(FALSE);
	if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
		this->m_MovInProg = true;
		this->m_btnExe.EnableWindow(FALSE);
		this->m_btnStop.EnableWindow(TRUE);
	}
	else{
		this->m_MovInProg = false;
		if((this->m_loggedUser <= 4 && this->m_loggedUser > 0) || this->m_loggedUser == SYSMGR)
			this->m_btnExe.EnableWindow(TRUE);
		this->m_btnStop.EnableWindow(FALSE);
	}

	this->m_lstMast.InsertColumn(1, _T("Type"), LVCFMT_CENTER, 70, 0);
	this->m_lstMast.InsertColumn(2, _T("Name"), LVCFMT_CENTER, 110, 0);
	this->m_lstCell.InsertColumn(1, _T("Type"), LVCFMT_CENTER, 70, 0);
	this->m_lstCell.InsertColumn(2, _T("Name"), LVCFMT_CENTER, 110, 0);

	this->m_progMov.SetStep(1);
	this->m_progMov.SetRange(0, PROG_STEPS);
	this->m_progMov.SetPos(0);

	this->m_refMast = MAIN_MAST;

	GetFALVOffset(&this->m_x_off, &this->m_y_off);

	this->m_falvlMounted = false;
	this->m_dimpMounted = false;

	this->ReadFloatPos();
	this->SetBmp();
	this->LoadSP();
	this->LoadMreq();
	this->LoadParam();
	this->LoadMast();
	this->LoadLimits();
	this->LoadCell();

	this->m_iconOK = theApp.LoadIcon(IDI_ICON_MSG_OK);
	this->m_iconWRN = theApp.LoadIcon(IDI_ICON_MSG_WRN);
	this->m_iconERR = theApp.LoadIcon(IDI_ICON_MSG_FAIL);

	this->m_begin = true;

	ret = SetTimer(TIMER_FAST_ID, TIMER_FAST_PERIOD, NULL);
	if(ret == 0)
		return(FALSE);

	ret = SetTimer(TIMER_SLOW_ID, TIMER_SLOW_PERIOD, NULL);
	if(ret == 0)
		return(FALSE);

	this->IsReady();

	this->UpdateData(FALSE);

	theApp.m_pDlg->m_ManModeOpened = true;

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

bool ManualMode::SetControls(void){
	RETSTAT		retSt;
	
	//Controllo l'utente//
	retSt = GetLoggedUserLevel(theApp.m_pDlg->m_opStation, &this->m_loggedUser);
	if(retSt != RT_NORMAL){
		//MessageBox("Unknown logged user.",  _T("Error"), MB_OK);
		_stprintf_s(this->m_msg, _T("Unknown logged user."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return false;
	}
	switch(this->m_loggedUser){
	case SYSMGR:
	case LEVEL1:
	case LEVEL2:
	case LEVEL3:
	case LEVEL4:
		this->m_btnExe.EnableWindow(true);
		this->m_btnExit.EnableWindow(true);
		this->m_btnStop.EnableWindow(true);
		break;
	case LEVEL5:
	
	
		this->m_btnExe.EnableWindow(false);
		this->m_btnExit.EnableWindow(true);
		this->m_btnStop.EnableWindow(true);
		break;
	
	default:
		this->m_btnExe.EnableWindow(false);
		this->m_btnExit.EnableWindow(true);
		this->m_btnStop.EnableWindow(true);
		break;
	}
	return true;
}

bool ManualMode::CheckUser(){
	RETSTAT		retSt;
	int			currentUser;
	
	currentUser = this->m_loggedUser;
	//Controllo l'utente//
	retSt = GetLoggedUserLevel(theApp.m_pDlg->m_opStation, &this->m_loggedUser);
	if(retSt != RT_NORMAL){
		//MessageBox("Unknown logged user.",  _T("Error"), MB_OK);
		_stprintf_s(this->m_msg, _T("Unknown logged user."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return false;
	}
	if(m_loggedUser != currentUser){
		SetControls();
		return true;
	}
	return false;
}


void ManualMode::OnTimer(UINT nIDEvent){
	RETCOND			retCnd;
	RETSTAT			retSt;
	TAGVALEX		tagVal;
	GENIDX			zoneIdx;
	ZONENAME		zoneName;
	SYSTEMTIME		sysTime;
	float			px;
	float			py;
	float			tot;
	float			part;
	TCHAR			userName[SZUNAM+1];
	TCHAR			tripStr[TRIP_DESCR_LEN];
	TCHAR			timeStr[32];
	int				mode;
	int				perc;
	int				vx;
	int				vy;
	int				flag;
	int				tripFlag;
	int				lastMov;
	bool			fix;
	bool			unfix;
	bool			fixOp;
	bool			startSaved;
	bool			lastOpOk;
	bool			lastOpNotOk;
	bool			tvrRight;
	bool			tvrLeft;
	bool			toDown;
	bool			toUp;
	bool			down;

	switch(nIDEvent){
	case TIMER_FAST_ID:
		//Check mov in progress//
		retSt = GetDynCurrentMode(&mode);
		if((retSt != R_NORMAL) || (mode != M_PROTECT))
			break;

		this->UpdateData(true);
		retCnd = TagRead(&theApp.m_pDlg->m_tiStart, &tagVal);
		if(retCnd != R_NORMAL)
			return;
		//if(CheckTrip()){
		//	this->m_btnResetTrip.EnableWindow(true);
			//this->UnselectMov();
		//}
		//else{
		//	this->m_btnResetTrip.EnableWindow(false);
		//}
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			startSaved = true;
			if(!this->m_MovInProg){
				//New movement started//
				switch(this->m_MovSelected){
				case MOV_NULL:
					break;
				case MOV_BRIDGE:
					retCnd = TagRead(&theApp.m_pDlg->m_tiBridgePos, &tagVal);
					if(retCnd == R_NORMAL)
						start_1 = tagVal.value.val_float;
					else
						startSaved = false;
					break;
				case MOV_TROLLEY:
					retCnd = TagRead(&theApp.m_pDlg->m_tiTrolleyPos, &tagVal);
					if(retCnd == R_NORMAL)
						start_1 = tagVal.value.val_float;
					else
						startSaved = false;
					break;
				case MOV_MM:
					retCnd = TagRead(&theApp.m_pDlg->m_tiMMPos, &tagVal);
					if(retCnd == R_NORMAL)
						start_1 = tagVal.value.val_float;
					else
						startSaved = false;
					break;
				case MOV_MM_CLUS:
				case MOV_MM_FIX:
					retCnd = TagRead(&theApp.m_pDlg->m_tiMMClusPos, &tagVal);
					if(retCnd == R_NORMAL)
						start_1 = tagVal.value.val_float;
					else
						startSaved = false;
					break;		
				case MOV_MM_ROT:
					retCnd = TagRead(&theApp.m_pDlg->m_tiMMRotPos, &tagVal);
					if(retCnd == R_NORMAL)
						start_1 = tagVal.value.val_float;
					else
						startSaved = false;
					break;
				case MOV_TV:
					retCnd = TagRead(&theApp.m_pDlg->m_tiTVPos, &tagVal);
					if(retCnd == R_NORMAL)
						start_1 = tagVal.value.val_float;
					else
						startSaved = false;
					break;
				case MOV_TV_ROT:
					retCnd = TagRead(&theApp.m_pDlg->m_tiTVRotPos, &tagVal);
					if(retCnd == R_NORMAL)
						start_1 = tagVal.value.val_float;
					else
						startSaved = false;
					break;
				case MOV_MM_DISPL:
					startSaved = true;
				default:
					break;				
				}
			}
			else{
				if(startSaved){
					switch(this->m_MovSelected){
					case MOV_NULL:
						break;
					case MOV_BRIDGE:
						if((this->m_Bridge_sp - start_1) >= 0)
							tot = this->m_Bridge_sp - start_1;
						else
							tot = start_1 - this->m_Bridge_sp;
						if((this->m_Bridge_pos - start_1) >= 0)
							part = this->m_Bridge_pos - start_1;
						else
							part = start_1 - this->m_Bridge_pos;

						perc = (int)((PROG_STEPS * part) / tot);
						break;
					case MOV_TROLLEY:
						if((this->m_Trolley_sp - start_1) >= 0)
							tot = this->m_Trolley_sp - start_1;
						else
							tot = start_1 - this->m_Trolley_sp;
						if((this->m_Trolley_pos - start_1) >= 0)
							part = this->m_Trolley_pos - start_1;
						else
							part = start_1 - this->m_Trolley_pos;

						perc = (int)((PROG_STEPS * part) / tot);
						break;
					case MOV_MM:
						if((this->m_MM_sp - start_1) >= 0)
							tot = this->m_MM_sp - start_1;
						else
							tot = start_1 - this->m_MM_sp;
						if((this->m_MM_pos - start_1) >= 0)
							part = this->m_MM_pos - start_1;
						else
							part = start_1 - this->m_MM_pos;

						perc = (int)((PROG_STEPS * part) / tot);
						break;
					case MOV_MM_CLUS:
					case MOV_MM_FIX:
						if((this->m_Clus_sp - start_1) >= 0)
							tot = this->m_Clus_sp - start_1;
						else
							tot = start_1 - this->m_Clus_sp;
						if((this->m_Clus_pos - start_1) >= 0)
							part = this->m_Clus_pos - start_1;
						else
							part = start_1 - this->m_Clus_pos;

						perc = (int)((PROG_STEPS * part) / tot);
						break;		
					case MOV_MM_ROT:
						if((this->m_MMR_sp - start_1) >= 0)
							tot = this->m_MMR_sp - start_1;
						else
							tot = start_1 - this->m_MMR_sp;
						if((this->m_MMR_pos - start_1) >= 0)
							part = this->m_MMR_pos - start_1;
						else
							part = start_1 - this->m_MMR_pos;

						perc = (int)((PROG_STEPS * part) / tot);
						break;
					case MOV_TV:
						if((this->m_TV_sp - start_1) >= 0)
							tot = this->m_TV_sp - start_1;
						else
							tot = start_1 - this->m_TV_sp;
						if((this->m_TV_pos - start_1) >= 0)
							part = this->m_TV_pos - start_1;
						else
							part = start_1 - this->m_TV_pos;

						perc = (int)((PROG_STEPS * part) / tot);
						break;
					case MOV_TV_ROT:
						if((this->m_TVR_sp - start_1) >= 0)
							tot = this->m_TVR_sp - start_1;
						else
							tot = start_1 - this->m_TVR_sp;
						if((this->m_TVR_pos - start_1) >= 0)
							part = this->m_TVR_pos - start_1;
						else
							part = start_1 - this->m_TVR_pos;

						perc = (int)((PROG_STEPS * part) / tot);
						break;
					case MOV_MM_DISPL:
						TagRead(&theApp.m_pDlg->m_tiMMDispl_lo, &tagVal);
						if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1)
							down = true;
						else
							down = false;

						TagRead(&theApp.m_pDlg->m_tiMMDispl_up, &tagVal);
						if((GtBit((unsigned short)(tagVal.value.val_di), 15) == 0) && !down)
							perc = 50;
						else
							perc = 0;

						break;
					default:
						break;				
					}
					if(this->m_MovSelected != MOV_FALV)
						this->m_progMov.SetPos(perc);
				}
			}
			this->m_MovInProg = true;
			this->m_btnExe.EnableWindow(FALSE);
			this->m_btnStop.EnableWindow(TRUE);
		}
		else{
			if(this->m_MovInProg){
				lastMov = this->m_MovSelected;
				//Movimento completato//
				Sleep(END_MOV_SLEEP);
				retCnd = TagRead(&theApp.m_pDlg->m_tiOP_OK, &tagVal);
				if(retCnd != R_NORMAL){
					_stprintf_s(this->m_msg, _T("Error readind \"LAST_OP_OK\" tag from Infi."));
					this->InsertMessage(MSG_ERR, this->m_msg);
					lastOpOk = false;
				}
				else{
					if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
						lastOpOk = true;
					else
						lastOpOk = false;
				}
				
				retCnd = TagRead(&theApp.m_pDlg->m_tiOP_NOT_OK, &tagVal);
				if(retCnd != R_NORMAL){
					_stprintf_s(this->m_msg, _T("Error readind \"LAST_OP_NOOK\" tag from Infi."));
					this->InsertMessage(MSG_ERR, this->m_msg);
					lastOpOk = false;
				}
				else{
					if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
						lastOpNotOk = true;
					else
						lastOpNotOk = false;
				}
				
				if(lastOpOk && !lastOpNotOk){
					_stprintf_s(this->m_msg, _T("Movement completed."));
					this->InsertMessage(MSG_OK, this->m_msg);
					this->UnselectMov();
					this->m_progMov.SetPos(100);
				}
				else if((!lastOpOk && lastOpNotOk) || (!lastOpOk && !lastOpNotOk)){
					retSt = GetTripInfo(&tripFlag, tripStr);
					if(retSt != RT_NORMAL){
						_stprintf_s(this->m_msg, _T("Error checking FHM trip status."));
						this->InsertMessage(MSG_ERR, this->m_msg);
						this->UnselectMov();
					}
					if(tripFlag == TRIP_OFF){
						_stprintf_s(this->m_msg, _T("Movement stopped."));
						this->InsertMessage(MSG_OK, this->m_msg);
						this->UnselectMov();
					}
					else{
						_stprintf_s(this->m_msg, _T("Movement failed. Trip: %s"), tripStr);
						this->InsertMessage(MSG_OK, this->m_msg);
						this->UnselectMov();
					}
				}
				else{
					_stprintf_s(this->m_msg, _T("Movement interrupted."));
					this->InsertMessage(MSG_OK, this->m_msg);
					this->UnselectMov();
				}
	
				//Zone
				retSt = GetMastPosInfiSC(this->m_refMast, &px, &py, &zoneIdx);
				if(retSt == RT_NORMAL){
					retSt = GetZoneName(zoneIdx, zoneName);
					if(retSt == RT_NORMAL)
						this->m_txtZone.Format(_T("%s"), zoneName);
					else
						this->m_txtZone.Format(_T("Unknown"));
				}
				else
					this->m_txtZone.Format(_T("Unknown"));

				//Cell//
				retSt = GetZonePhisVirtCoord(px, py, &vx, &vy);
				if(retSt != RT_NORMAL){
					this->m_txtVirtCoo.Format(_T("(---,---)"));
					if(this->m_onCell == true){
						this->m_vx = 0;
						this->m_vy = 0;
					}
					this->m_onCell = false;
				}
				else{
					this->m_txtVirtCoo.Format(_T("(%d,%d)"), vx, vy);
					if((this->m_vx != vx) || (this->m_vy != vy)){
						this->m_vx = vx;
						this->m_vy = vy;
					}
					this->m_onCell = true;
				}
				this->OnCellChanged(this->m_vx, this->m_vy);

				/*
				if((this->m_onCell) && ((lastMov == MOV_BRIDGE) || (lastMov == MOV_TROLLEY))){
					retSt = GetZSetPoints(&mastSp, &intWeight, &TVSp, &totWeight);
					if(retSt == RT_NORMAL){
						retSt = VotedTagWrite(TWN_MM_INT_LOOSE, 0, intWeight, FLT_VALUE);		
						retSt = VotedTagWrite(TWN_MM_TOT_LOOSE, 0, totWeight, FLT_VALUE);	
					}
				}
				*/
				this->UpdateData(false);
			}
			this->m_MovInProg = false;
			if((this->m_loggedUser <= 4 && this->m_loggedUser > 0) || this->m_loggedUser == SYSMGR)
				this->m_btnExe.EnableWindow(TRUE);
			this->m_btnStop.EnableWindow(FALSE);
			
		}

		//BRIDGE
		retCnd = TagRead(&theApp.m_pDlg->m_tiBridgePos, &tagVal);
		if(retCnd != R_NORMAL)
			return;
		
		this->m_Bridge_pos = tagVal.value.val_float;
		_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
		this->m_Bridge_pos_c.SetWindowText(this->m_valStr);

		//TROLLEY
		retCnd = TagRead(&theApp.m_pDlg->m_tiTrolleyPos, &tagVal);
		if(retCnd != R_NORMAL)
			return;
		
		this->m_Trolley_pos = tagVal.value.val_float;
		_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
		this->m_Trolley_pos_c.SetWindowText(this->m_valStr);

		//MM POS
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMPos, &tagVal);
		if(retCnd != R_NORMAL)
			return;
		
		this->m_MM_pos = tagVal.value.val_float;
		_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
		this->m_MM_pos_c.SetWindowText(this->m_valStr);

		//MM FIX
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMFixOp, &tagVal);
		if(retCnd != R_NORMAL)
			return;

		if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
			fixOp = true;
		else
			fixOp = false;

		retCnd = TagRead(&theApp.m_pDlg->m_tiMMFixCl, &tagVal);
		if(retCnd != R_NORMAL)
			return;
		
		if((GtBit((short int)(tagVal.value.val_di), 15) == 1) && !fixOp)
			this->m_MM_fix_pos.Format(_T("CLOSED"));
		else if((GtBit((short int)(tagVal.value.val_di), 15) == 0) && fixOp)
			this->m_MM_fix_pos.Format(_T("OPEN"));
		else
			this->m_MM_fix_pos.Format(_T("----"));

		//MM CLUS
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMClusPos, &tagVal);
		if(retCnd != R_NORMAL)
			return;
		
		this->m_Clus_pos = tagVal.value.val_float;
		_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
		this->m_Clus_pos_c.SetWindowText(this->m_valStr);

		//MM ROT
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMRotPos, &tagVal);
		if(retCnd != R_NORMAL)
			return;
		
		this->m_MMR_pos = tagVal.value.val_float;
		_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
		this->m_MMR_pos_c.SetWindowText(this->m_valStr);

		//TV POS
		retCnd = TagRead(&theApp.m_pDlg->m_tiTVPos, &tagVal);
		if(retCnd != R_NORMAL)
			return;
		
		this->m_TV_pos = tagVal.value.val_float;
		_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
		this->m_TV_pos_c.SetWindowText(this->m_valStr);

		//TV ROT
		retCnd = TagRead(&theApp.m_pDlg->m_tiTVRotPos, &tagVal);
		if(retCnd != R_NORMAL)
			return;
		
		this->m_TVR_pos = tagVal.value.val_float;
		_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
		this->m_TVR_pos_c.SetWindowText(this->m_valStr);

		//MM DISPL//
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMDispl_lo, &tagVal);
		if(retCnd != R_NORMAL)
			return;

		if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
			down = true;
		else
			down = false;

		retCnd = TagRead(&theApp.m_pDlg->m_tiMMDispl_up, &tagVal);
		if(retCnd != R_NORMAL)
			return;
		
		if((GtBit((short int)(tagVal.value.val_di), 15) == 1) && !down)
			this->m_MMDisp_pos.Format(_T("UP"));
		else if((GtBit((short int)(tagVal.value.val_di), 15) == 0) && down)
			this->m_MMDisp_pos.Format(_T("DOWN"));
		else
			this->m_MMDisp_pos.Format(_T("----"));

		//FALV POS//
		retCnd = TagRead(&theApp.m_pDlg->m_tiFALV_down, &tagVal);
		if(retCnd != R_NORMAL)
			return;

		if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
			down = true;
		else
			down = false;

		retCnd = TagRead(&theApp.m_pDlg->m_tiFALV_up, &tagVal);
		if(retCnd != R_NORMAL)
			return;
		
		if((GtBit((short int)(tagVal.value.val_di), 15) == 1) && !down)
			this->m_FalvPos.Format(_T("UPPER"));
		else if((GtBit((short int)(tagVal.value.val_di), 15) == 0) && down)
			this->m_FalvPos.Format(_T("LOWER"));
		else
			this->m_FalvPos.Format(_T("----"));

		//BRIDGE SP//
		retCnd = TagRead(&theApp.m_pDlg->m_tiBridgeSp, &tagVal);
		if(retCnd == R_NORMAL){
			_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
			this->m_Bridge_sp_rc.SetWindowText(this->m_valStr);
			this->m_Bridge_sp = tagVal.value.val_float;
		}
		else{
			this->m_Bridge_sp_rc.SetWindowText(_T("BAD"));
			this->m_Bridge_sp = (float)(-1);
		}
		
		//TROLLEY SP//
		retCnd = TagRead(&theApp.m_pDlg->m_tiTrolleySp, &tagVal);
		if(retCnd == R_NORMAL){
			_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
			this->m_Trolley_sp_rc.SetWindowText(this->m_valStr);
			this->m_Trolley_sp = tagVal.value.val_float;
		}
		else{
			this->m_Trolley_sp_rc.SetWindowText(_T("BAD"));
			this->m_Trolley_sp = (float)(-1);
		}
		
		//MM SP//
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMSp, &tagVal);
		if(retCnd == R_NORMAL){
			_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
			this->m_MM_sp_rc.SetWindowText(this->m_valStr);
			this->m_MM_sp = tagVal.value.val_float;
		}
		else{
			this->m_MM_sp_rc.SetWindowText(_T("BAD"));
			this->m_MM_sp = (float)(-1);
		}
		
		//MM Cluster//
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMClusSp, &tagVal);
		if(retCnd == R_NORMAL){
			_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
			this->m_Clus_sp_rc.SetWindowText(this->m_valStr);
			this->m_Clus_sp = tagVal.value.val_float;
		}
		else{
			this->m_Clus_sp_rc.SetWindowText(_T("BAD"));
			this->m_Clus_sp = (float)(-1);
		}
		
		//TV SP//
		retCnd = TagRead(&theApp.m_pDlg->m_tiTVSp, &tagVal);
		if(retCnd == R_NORMAL){
			_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
			this->m_TV_sp_rc.SetWindowText(this->m_valStr);
			this->m_TV_sp = tagVal.value.val_float;
		}
		else{
			this->m_TV_sp_rc.SetWindowText(_T("BAD"));
			this->m_TV_sp = (float)(-1);
		}
		
		//TVR SP//
		retCnd = TagRead(&theApp.m_pDlg->m_tiTVRotSp, &tagVal);
		if(retCnd == R_NORMAL){
			_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
			this->m_TVR_sp_rc.SetWindowText(this->m_valStr);
			this->m_TVR_sp = tagVal.value.val_float;
		}
		else{
			this->m_TVR_sp_rc.SetWindowText(_T("BAD"));
			this->m_TVR_sp = (float)(-1);
		}

		//MM ROT SP//
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMRotSp, &tagVal);
		if(retCnd == R_NORMAL){
			_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
			this->m_MMR_sp_rc.SetWindowText(this->m_valStr);
			this->m_MMR_sp = tagVal.value.val_float;
		}
		else{
			this->m_MMR_sp_rc.SetWindowText(_T("BAD"));
			this->m_MMR_sp = (float)(-1);
		}


		//MM FIX//
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMFixReq, &tagVal);
		if(retCnd != R_NORMAL){
			this->m_MMFixSp.Format(_T("BAD"));
		}
		else{
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				fix = true;
			else
				fix = false;
		}

		retCnd = TagRead(&theApp.m_pDlg->m_tiMMUnfixReq, &tagVal);
		if(retCnd != R_NORMAL){
			this->m_MMFixSp.Format(_T("BAD"));
		}
		else{
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				unfix = true;
			else
				unfix = false;
		}

		if(fix && !unfix)
			this->m_MMFixSp.Format(_T("CLOSE"));
		else if(!fix && unfix)
			this->m_MMFixSp.Format(_T("OPEN"));
		else if(!fix && !unfix)
			this->m_MMFixSp.Format(_T(""));
		else
			this->m_MMFixSp.Format(_T("BAD"));

		//MM DISPL SP//
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMDispl_lo_req, &tagVal);
		if(retCnd != R_NORMAL){
			this->m_MMFixSp.Format(_T("BAD"));
		}
		else{
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				fix = true;
			else
				fix = false;
		}

		retCnd = TagRead(&theApp.m_pDlg->m_tiMMDispl_hi_req, &tagVal);
		if(retCnd != R_NORMAL){
			this->m_MMFixSp.Format(_T("BAD"));
		}
		else{
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				unfix = true;
			else
				unfix = false;
		}

		if(fix && !unfix)
			this->m_MMDisp_sp.Format(_T("DOWN"));
		else if(!fix && unfix)
			this->m_MMDisp_sp.Format(_T("UP"));
		else if(!fix && !unfix)
			this->m_MMDisp_sp.Format(_T("---"));
		else
			this->m_MMDisp_sp.Format(_T("BAD"));

		//FALVL SP//
		retCnd = TagRead(&theApp.m_pDlg->m_tiFALVL_dwn_req, &tagVal);
		if(retCnd != R_NORMAL){
			this->m_MMFixSp.Format(_T("BAD"));
		}
		else{
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				fix = true;
			else
				fix = false;
		}

		retCnd = TagRead(&theApp.m_pDlg->m_tiFALVL_up_req, &tagVal);
		if(retCnd != R_NORMAL){
			this->m_MMFixSp.Format(_T("BAD"));
		}
		else{
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				unfix = true;
			else
				unfix = false;
		}

		if(fix && !unfix)
			this->m_FALVL_sp.Format(_T("DOWN"));
		else if(!fix && unfix)
			this->m_FALVL_sp.Format(_T("UP"));
		else if(!fix && !unfix)
			this->m_FALVL_sp.Format(_T("---"));
		else
			this->m_FALVL_sp.Format(_T("BAD"));

		//BRIDGE ACTUAL SPEED//
		retCnd = TagRead(&theApp.m_pDlg->m_tiBridge_v, &tagVal);
		if(retCnd == R_NORMAL)
			this->m_Bridge_v.Format(_T("%.1f"), tagVal.value.val_float);
		else
			this->m_TVR_sp = (float)(-1);

		//TROLLEY ACTUAL SPEED//
		retCnd = TagRead(&theApp.m_pDlg->m_tiTrolley_v, &tagVal);
		if(retCnd == R_NORMAL)
			this->m_Trolley_v.Format(_T("%.1f"), tagVal.value.val_float);
		else
			this->m_TVR_sp = (float)(-1);

		//MAIN MAST ACTUAL SPEED//
		retCnd = TagRead(&theApp.m_pDlg->m_tiMM_v, &tagVal);
		if(retCnd == R_NORMAL)
			this->m_MM_v.Format(_T("%.1f"), tagVal.value.val_float);
		else
			this->m_TVR_sp = (float)(-1);

		//MM CLUSTER ACTUAL SPEED//
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMClus_v, &tagVal);
		if(retCnd == R_NORMAL)
			this->m_MMClus_v.Format(_T("%.1f"), tagVal.value.val_float);
		else
			this->m_TVR_sp = (float)(-1);

		//MM FIX ACTUAL SPEED//
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMFix_v, &tagVal);
		if(retCnd == R_NORMAL)
			this->m_MMFix_v.Format(_T("%.1f"), tagVal.value.val_float);
		else
			this->m_TVR_sp = (float)(-1);

		//TV MAST ACTUAL SPEED//
		retCnd = TagRead(&theApp.m_pDlg->m_tiTV_v, &tagVal);
		if(retCnd == R_NORMAL)
			this->m_TV_v.Format(_T("%.1f"), tagVal.value.val_float);
		else
			this->m_TVR_sp = (float)(-1);

		//TV ROTATION DIRECTION//
		retCnd = TagRead(&theApp.m_pDlg->m_tiTVR_l, &tagVal);
		if(retCnd == R_NORMAL){
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				tvrLeft = true;
			else
				tvrLeft = false;
		}
		else
			tvrLeft = false;

		retCnd = TagRead(&theApp.m_pDlg->m_tiTVR_r, &tagVal);
		if(retCnd == R_NORMAL){
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				tvrRight = true;
			else
				tvrRight = false;
		}
		else
			tvrRight = false;

		if((!tvrRight) && (tvrLeft))
			this->m_TVR_dir.Format(_T("RUN BACKW."));
		else if((tvrRight) && (!tvrLeft))
			this->m_TVR_dir.Format(_T("RUN FORW."));
		else if((tvrRight) && (tvrLeft))
			this->m_TVR_dir.Format(_T("UNKNOWN"));
		else if((!tvrRight) && (!tvrLeft))
			this->m_TVR_dir.Format(_T(""));

		//MM DISPLACING DIRECTION//
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMDisplToUp, &tagVal);
		if(retCnd == R_NORMAL){
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				toUp = true;
			else
				toUp = false;
		}
		else
			toUp = false;

		retCnd = TagRead(&theApp.m_pDlg->m_tiMMDisplToDown, &tagVal);
		if(retCnd == R_NORMAL){
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				toDown = true;
			else
				toDown = false;
		}
		else
			toDown = false;

		if((!toDown) && (toUp))
			this->m_MMDisp_dir.Format(_T("UPWARD"));
		else if((toDown) && (!toUp))
			this->m_MMDisp_dir.Format(_T("DOWNWARD"));
		else if((toDown) && (toUp))
			this->m_MMDisp_dir.Format(_T("UNKNOWN"));
		else if((!toDown) && (!toUp))
			this->m_MMDisp_dir.Format(_T(""));

		//MM ACTUAL WEIGHT//
		retCnd = TagRead(&theApp.m_pDlg->m_tiMM_act, &tagVal);
		if(retCnd == R_NORMAL)
			this->m_wgMM_act.Format(_T("%.1f"), tagVal.value.val_float);
		else
			this->m_wgMM_act.Format(_T("OLD"));

		//MM EXPECTED WEIGHT//
		retCnd = TagRead(&theApp.m_pDlg->m_tiMM_exp, &tagVal);
		if(retCnd == R_NORMAL)
			this->m_wgMM_exp.Format(_T("%.1f"), tagVal.value.val_float);
		else
			this->m_wgMM_exp.Format(_T("OLD"));

		//CLUSTER ACTUAL WEIGHT//
		retCnd = TagRead(&theApp.m_pDlg->m_tiCl_act, &tagVal);
		if(retCnd == R_NORMAL)
			this->m_wgCl_act.Format(_T("%.1f"), tagVal.value.val_float);
		else
			this->m_wgCl_act.Format(_T("OLD"));

		//CLUSTER EXPECTED WEIGHT//
		retCnd = TagRead(&theApp.m_pDlg->m_tiCl_exp, &tagVal);
		if(retCnd == R_NORMAL)
			this->m_wgCl_exp.Format(_T("%.1f"), tagVal.value.val_float);
		else
			this->m_wgCl_exp.Format(_T("OLD"));

		//DISPLACING LC A//
		retCnd = TagRead(&theApp.m_pDlg->m_tiDispl_A, &tagVal);
		if(retCnd == R_NORMAL)
			this->m_displ_A.Format(_T("%.1f"), tagVal.value.val_float);
		else
			this->m_displ_A.Format(_T("OLD"));
		
		//DISPLACING LC B//
		retCnd = TagRead(&theApp.m_pDlg->m_tiDispl_B, &tagVal);
		if(retCnd == R_NORMAL)
			this->m_displ_B.Format(_T("%.1f"), tagVal.value.val_float);
		else
			this->m_displ_B.Format(_T("OLD"));	

		GetLocalTime(&sysTime);
		_stprintf_s(timeStr, _T("%02d:%02d.%02d - %02d/%02d/%d"), sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wDay, sysTime.wMonth, sysTime.wYear);
		this->m_sysTime.SetWindowText(timeStr);
		this->m_sysTime.UpdateData(false);
		//this->_UpdateData(UD_DOWN_FALSE);
		if(this->m_MovInProg || this->m_begin){
			this->m_begin = false;	
		}

		this->SetBmp();

		this->UpdateData(false);
		

		break;
	case TIMER_SLOW_ID:
		retCnd = TagRead(&theApp.m_pDlg->m_tiStart, &tagVal);
		if(retCnd != R_NORMAL)
			return;
		//if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			//USER
			retSt = GetLoggedUserName(theApp.m_pDlg->m_opStation, userName);
			CheckUser();
			if((this->m_loggedUser > 4 || this->m_loggedUser < 1) && this->m_loggedUser != SYSMGR){
				this->m_btnExe.EnableWindow(false);
				this->m_bMntMM.EnableWindow(false);
				this->m_bMmtTVM.EnableWindow(false);
				this->m_bMntFALV.EnableWindow(false);
				this->m_bMntDimp.EnableWindow(false);
				//this->DestroyWindow();
			}
			else{
				this->m_bMntMM.EnableWindow(true);
				this->m_bMmtTVM.EnableWindow(true);
				this->m_bMntFALV.EnableWindow(true);
				this->m_bMntDimp.EnableWindow(true);
			}
			if(retSt == RT_NORMAL)
				this->m_txtUser.Format(_T("%s"), userName);
			else
				this->m_txtUser.Format(_T("Unknown"));

			//OP MODE
			retSt = GetDynCurrentMode(&mode);
			if(retSt == RT_NORMAL){
				switch(mode){
				case M_NO_MODE:
					this->m_txtOpMode.Format(_T("No mode"));
					break;
				case M_AUTOMATIC:
					this->m_txtOpMode.Format(_T("Automatic"));
					break;
				case M_SEMIAUTO:
					this->m_txtOpMode.Format(_T("Semi-Automatic"));
					break;
				case M_PROTECT:
					this->m_txtOpMode.Format(_T("Protected Manual"));
					break;
				case M_MAN_SINGLE_CH:
					this->m_txtOpMode.Format(_T("Manual single Channel"));
					break;
				case M_MAN_WO_INT:
					this->m_txtOpMode.Format(_T("Manual without Interlocks"));
					break;
				default:
					this->m_txtOpMode.Format(_T("Unknown mode"));
					break;
				}
			}
			else
				this->m_txtOpMode.Format(_T("Unknown mode"));

			//Zone
			retSt = GetMastPosInfiSC(this->m_refMast, &px, &py, &zoneIdx);
			if(retSt == RT_NORMAL){
				retSt = GetZoneName(zoneIdx, zoneName);
				if(retSt == RT_NORMAL)
					this->m_txtZone.Format(_T("%s"), zoneName);
				else
					this->m_txtZone.Format(_T("Unknown"));
			}
			else
				this->m_txtZone.Format(_T("Unknown"));
			
			retSt = sysInSimulation(&flag);
			if(retSt != RT_NORMAL)
				this->m_txtZone.Format(_T("ERR"));
			else{
				if(flag == 1)
					this->m_sysStatus.Format(_T("SIMULATION"));
				else
					this->m_sysStatus.Format(_T("CONTROL"));
			}

			//Cell//
			retSt = GetZonePhisVirtCoord(px, py, &vx, &vy);
			if(retSt != RT_NORMAL){
				this->m_txtVirtCoo.Format(_T("(---,---)"));
				if(this->m_onCell == true){
					this->m_vx = 0;
					this->m_vy = 0;
				}
				this->m_onCell = false;
			}
			else{
				this->m_txtVirtCoo.Format(_T("(%d,%d)"), vx, vy);
				if((this->m_vx != vx) || (this->m_vy != vy)){
					this->m_vx = vx;
					this->m_vy = vy;
				}
				this->m_onCell = true;
			}
			this->OnCellChanged(this->m_vx, this->m_vy);

			SetMimicMastAndCellSituation();

			this->_UpdateData(false);
		break;
	default:
		break;

	}

	CDialog::OnTimer(nIDEvent);
}

bool ManualMode::LoadSP(){
	RETCOND			retCnd;
	TAGVALEX		tagVal;
	bool			fix;
	bool			unfix;
	bool			up;
	bool			down;

	retCnd = TagRead(&theApp.m_pDlg->m_tiBridgeSp, &tagVal);
	if(retCnd == R_NORMAL){
		_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
		this->m_Bridge_sp_rc.SetWindowText(this->m_valStr);
		this->m_Bridge_sp = tagVal.value.val_float;
		this->m_Bridge_sp_c.SetWindowText(this->m_valStr);
	}
	else{
		_stprintf_s(this->m_valStr, _T("BAD"));
		this->m_Bridge_sp_rc.SetWindowText(this->m_valStr);
		this->m_Bridge_sp = (float)(-1);
		this->m_Bridge_sp_c.SetWindowText(this->m_valStr);
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiTrolleySp, &tagVal);
	if(retCnd == R_NORMAL){
		_stprintf_s(this->m_valStr, _T("%.1f"));
		this->m_Trolley_sp_rc.SetWindowText(this->m_valStr);
		this->m_Trolley_sp = tagVal.value.val_float;
		this->m_Trolley_sp_c.SetWindowText(this->m_valStr);
	}
	else{
		_stprintf_s(this->m_valStr, _T("BAD"));
		this->m_Trolley_sp_rc.SetWindowText(this->m_valStr);
		this->m_Trolley_sp = (float)(-1);
		this->m_Trolley_sp_c.SetWindowText(this->m_valStr);
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiMMRotSp, &tagVal);
	if(retCnd == R_NORMAL){
		_stprintf_s(this->m_valStr, _T("%.1f"));
		this->m_MMR_sp_rc.SetWindowText(this->m_valStr);
		this->m_MMR_sp = tagVal.value.val_float;
		this->m_MMR_sp_c.SetWindowText(this->m_valStr);
	}
	else{
		_stprintf_s(this->m_valStr, _T("BAD"));
		this->m_MMR_sp_rc.SetWindowText(this->m_valStr);
		this->m_MMR_pos = (float)(-1);
		this->m_MMR_sp_c.SetWindowText(this->m_valStr);
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiMMSp, &tagVal);
	if(retCnd == R_NORMAL){
		_stprintf_s(this->m_valStr, _T("%.1f"));
		this->m_MM_sp_rc.SetWindowText(this->m_valStr);
		this->m_MM_sp = tagVal.value.val_float;
		this->m_MM_sp_c.SetWindowText(this->m_valStr);
	}
	else{
		_stprintf_s(this->m_valStr, _T("BAD"));
		this->m_MM_sp_rc.SetWindowText(this->m_valStr);
		this->m_MM_sp = (float)(-1);
		this->m_MM_sp_c.SetWindowText(this->m_valStr);
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiMMClusSp, &tagVal);
	if(retCnd == R_NORMAL){
		_stprintf_s(this->m_valStr, _T("%.1f"));
		this->m_Clus_sp_rc.SetWindowText(this->m_valStr);
		this->m_Clus_sp = tagVal.value.val_float;
		this->m_Clus_sp_c.SetWindowText(this->m_valStr);
	}
	else{
		_stprintf_s(this->m_valStr, _T("BAD"));
		this->m_Clus_sp_rc.SetWindowText(this->m_valStr);
		this->m_Clus_sp = (float)(-1);
		this->m_Clus_sp_c.SetWindowText(this->m_valStr);
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiTVSp, &tagVal);
	if(retCnd == R_NORMAL){
		_stprintf_s(this->m_valStr, _T("%.1f"));
		this->m_TV_sp_rc.SetWindowText(this->m_valStr);
		this->m_TV_sp = tagVal.value.val_float;
		this->m_TV_sp_c.SetWindowText(this->m_valStr);
	}
	else{
		_stprintf_s(this->m_valStr, _T("BAD"));
		this->m_TV_sp_rc.SetWindowText(this->m_valStr);
		this->m_TV_sp = (float)(-1);
		this->m_TV_sp_c.SetWindowText(this->m_valStr);
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiTVRotSp, &tagVal);
	if(retCnd == R_NORMAL){
		_stprintf_s(this->m_valStr, _T("%.1f"));
		this->m_TVR_sp_rc.SetWindowText(this->m_valStr);
		this->m_TVR_sp = tagVal.value.val_float;
		this->m_TVR_sp_c.SetWindowText(this->m_valStr);
	}
	else{
		_stprintf_s(this->m_valStr, _T("BAD"));
		this->m_TVR_sp_rc.SetWindowText(this->m_valStr);
		this->m_TVR_sp = (float)(-1);
		this->m_TVR_sp_c.SetWindowText(this->m_valStr);
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiMMFixReq, &tagVal);
	if(retCnd != R_NORMAL){
		this->m_MMFixSp.Format(_T("BAD"));
	}
	else{
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
			fix = true;
		else
			fix = false;
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiMMUnfixReq, &tagVal);
	if(retCnd != R_NORMAL){
		this->m_MMFixSp.Format(_T("BAD"));
	}
	else{
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
			unfix = true;
		else
			unfix = false;
	}

	if(fix && !unfix){
		this->m_MMFixSp.Format(_T("CLOSE"));
		this->m_cmb_MMFix_sp.SetCurSel(CMB_MM_FIX_CL);
	}
	else if(!fix && unfix){
		this->m_MMFixSp.Format(_T("OPEN"));
		this->m_cmb_MMFix_sp.SetCurSel(CMB_MM_FIX_OP);
	}
	else if(!fix && !unfix)
		this->m_MMFixSp.Format(_T(""));
	else
		this->m_MMFixSp.Format(_T("BAD"));

	retCnd = TagRead(&theApp.m_pDlg->m_tiMMDispl_lo_req, &tagVal);
	if(retCnd != R_NORMAL){
		this->m_MMDisp_sp.Format(_T("BAD"));
	}
	else{
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
			down = true;
		else
			down = false;
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiMMDispl_hi_req, &tagVal);
	if(retCnd != R_NORMAL){
		this->m_MMDisp_sp.Format(_T("BAD"));
	}
	else{
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
			up = true;
		else
			up = false;
	}

	if(up && !down){
		this->m_MMDisp_sp.Format(_T("UP"));
		this->m_cmbMMDispl.SetCurSel(CMB_MM_DISPL_UP);
	}
	else if(!up && down){
		this->m_MMDisp_sp.Format(_T("DOWN"));
		this->m_cmbMMDispl.SetCurSel(CMB_MM_DISPL_DOWN);
	}
	else if(!up && !down)
		this->m_MMDisp_sp.Format(_T(""));
	else
		this->m_MMDisp_sp.Format(_T("BAD"));

	retCnd = TagRead(&theApp.m_pDlg->m_tiFALVL_dwn_req, &tagVal);
	if(retCnd != R_NORMAL){
		this->m_MMDisp_sp.Format(_T("BAD"));
	}
	else{
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
			down = true;
		else
			down = false;
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiFALVL_up_req, &tagVal);
	if(retCnd != R_NORMAL){
		this->m_MMDisp_sp.Format(_T("BAD"));
	}
	else{
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
			up = true;
		else
			up = false;
	}

	if(up && !down){
		this->m_FALVL_sp.Format(_T("UP"));
		this->m_cmbFALVL_sp.SetCurSel(CMB_MM_DISPL_UP);
	}
	else if(!up && down){
		this->m_FALVL_sp.Format(_T("DOWN"));
		this->m_cmbFALVL_sp.SetCurSel(CMB_MM_DISPL_DOWN);
	}
	else if(!up && !down)
		this->m_FALVL_sp.Format(_T(""));
	else
		this->m_FALVL_sp.Format(_T("BAD"));

	this->UpdateData(false);

	this->OnSpChanged(0, 0);
	
	return(true);
}

void ManualMode::OnExe(){
	RETSTAT			retSt;
	RETCOND			retCnd;
	TAGVALEX		tagVal;	
	HCURSOR			prev;
	CString			str;
	float			sp_1;
	float			clgrWeight;
	float			objWeight;
	float			weight;
	float			lowLim;
	float			highLim;
	int				retInt;
	int				retInt_3;
	int				j;
	int				opMode;
	int				spCount;
	int				local;
	bool			spOk;

	if(CheckUser())
		return;
	
	GetDynCurrentMode(&opMode);

	GetStationID(&local);

	if(opMode != M_PROTECT){
		_stprintf_s(this->m_msg, _T("Invalid operating mode to move FHM from this page."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	if(this->m_MovInProg){
		//MessageBox("Another movement is currently in progress.\nEnd movement and restart.", "Warning", MB_OK | MB_ICONINFORMATION);
		_stprintf_s(this->m_msg, _T("Another movement is currently in progress. End movement and restart."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	SetMimicMastAndCellSituation();

	switch(this->m_MovSelected){
	case MOV_NULL:
		//MessageBox("Please select a movement.", "Warning", MB_OK | MB_ICONINFORMATION);
		_stprintf_s(this->m_msg, _T("Please select a movement."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		break;
	case MOV_BRIDGE:
		this->m_Bridge_sp_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Bridge movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &sp_1);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Bridge movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_Bridge_pos_LL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Bridge Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &lowLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Bridge Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_Bridge_pos_HL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Bridge Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &highLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Bridge Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retInt = this->CheckSp(sp_1, this->m_Bridge_pos, lowLim, highLim);
		switch(retInt){
		case SP_OK:
			retCnd = TagRead(&theApp.m_pDlg->m_tiBridgeMreq, &tagVal);
			if(retCnd != R_NORMAL){
				//MessageBox("Cannot check Bridge movement request value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot check Bridge movement request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			if(GtBit((short int)(tagVal.value.val_di), 15) == 0){
				//MessageBox("Bridge movement is not selected properly.",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Bridge movement is not selected properly."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			retSt = VotedTagWrite(TWN_BRIDGE_SP, 0, sp_1, FLT_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write Bridge setpoint value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write Bridge setpoint value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			//Rilettura SP//
			prev = SetCursor(this->m_curHour);
			Sleep(SP_CHECK_INIT_WAIT);
			spOk = false;
			spCount = SP_CHECK_CYCLES;
			while(spCount > 0){
				Sleep(SP_CHECK_WAIT);

				retCnd = TagRead(&theApp.m_pDlg->m_tiBridgeSp, &tagVal);
				if(retCnd != R_NORMAL)
					spOk = false;
				else{
					if((tagVal.value.val_float < (float)(sp_1 - TOLL_MM)) || (tagVal.value.val_float > (float)(sp_1 + TOLL_MM)))
						spOk = false;
					else
						spOk = true;
				}

				spCount--;

				if(spOk)
					break;
			}

			prev = SetCursor(this->m_curNormal);

			if(!spOk){
				_stprintf_s(this->m_msg, _T("Error setting bridge set point value; try again please."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			SetMasterStation(local);

			retSt = VotedTagWrite(TWN_START_MOV, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write Start tag",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write Start tag."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			_stprintf_s(this->m_msg, _T("Bridge movement started."));
			this->InsertMessage(MSG_OK, this->m_msg);

			Sleep(MAN_SLEEP_AFTER_START);

			break;
		case SP_OUT_OF_BOUND:
			//MessageBox("Bridge set point inserted is out of bounds.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Bridge set point inserted is out of bounds."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
			break;
		case SP_IN_POS:
			//MessageBox("Bridge is already is the selected position.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Bridge is already is the selected position."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
			break;
		default:
			break;
		}
		break;
	case MOV_TROLLEY:
		this->m_Trolley_sp_c.GetWindowText(str);
		if(!_tcscmp(str, _T(""))){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Trolley movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &sp_1);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Trolley movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_Trolley_pos_LL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Trolley Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &lowLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Trolley Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_Trolley_pos_HL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Trolley Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &highLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Trolley Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retInt = this->CheckSp(sp_1, this->m_Trolley_pos, lowLim, highLim);
		switch(retInt){
		case SP_OK:
			retCnd = TagRead(&theApp.m_pDlg->m_tiTrolleyMreq, &tagVal);
			if(retCnd != R_NORMAL){
				//MessageBox("Cannot check Trolley movement request value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot check Trolley movement request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			if(GtBit((short int)(tagVal.value.val_di), 15) == 0){
				//MessageBox("Trolley movement is not selected properly.",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Trolley movement is not selected properly."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			retSt = VotedTagWrite(TWN_TROLLEY_SP, 0, sp_1, FLT_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write Trolley setpoint value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write Trolley setpoint value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			//Rilettura SP//
			prev = SetCursor(this->m_curHour);
			Sleep(SP_CHECK_INIT_WAIT);
			spOk = false;
			spCount = SP_CHECK_CYCLES;
			while(spCount > 0){
				Sleep(SP_CHECK_WAIT);

				retCnd = TagRead(&theApp.m_pDlg->m_tiTrolleySp, &tagVal);
				if(retCnd != R_NORMAL)
					spOk = false;
				else{
					if((tagVal.value.val_float < (float)(sp_1 - TOLL_MM)) || (tagVal.value.val_float > (float)(sp_1 + TOLL_MM)))
						spOk = false;
					else
						spOk = true;
				}

				spCount--;

				if(spOk)
					break;
			}

			prev = SetCursor(this->m_curNormal);

			if(!spOk){
				_stprintf_s(this->m_msg, _T("Error setting trolley set point value; try again please."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			SetMasterStation(local);

			retSt = VotedTagWrite(TWN_START_MOV, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write Start tag",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write Start tag."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			_stprintf_s(this->m_msg, _T("Trolley movement started."));
			this->InsertMessage(MSG_OK, this->m_msg);

			Sleep(MAN_SLEEP_AFTER_START);
			break;
		case SP_OUT_OF_BOUND:
			//MessageBox("Bridge set point inserted is out of bounds.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Inserted trolley set point is out of bounds."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
			break;
		case SP_IN_POS:
			//MessageBox("Bridge is already is the selected position.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Trolley is already is the selected position."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
			break;
		default:
			break;
		}	
		break;
	case MOV_MM:
		this->m_MM_sp_c.GetWindowText(str);
		if(!_tcscmp(str, _T(""))){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Main mast movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &sp_1);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Main mast movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_MM_pos_LL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &lowLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_MM_pos_HL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &highLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retInt = this->CheckSp((float)(sp_1), (float)(this->m_MM_pos), lowLim, (float)(highLim + (double)(MM_SP_LOWER_TOLL)));
		switch(retInt){
		case SP_OK:
			retCnd = TagRead(&theApp.m_pDlg->m_tiMMMreq, &tagVal);
			if(retCnd != R_NORMAL){
				//MessageBox("Cannot check Main Mast movement request value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot check Main Mast movement request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			if(GtBit((short int)(tagVal.value.val_di), 15) == 0){
				//MessageBox("Trolley movement is not selected properly.",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Main mast FA gripper movement is not selected properly."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			

			retSt = VotedTagWrite(TWN_FAGR_SP, 0, sp_1, FLT_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write Main Mast setpoint value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast setpoint value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			/*
			if(sp_1 > this->m_MM_pos){
				retSt = GetZSetPoints(&mastSp, &mastIntWeight, &tvSp, &mastTotWeight);
				if(retSt != RT_NORMAL){
					_stprintf_s(this->m_msg, "Cannot retrieve Main mast op. quotes, using default int.set points setting.");
					this->InsertMessage(MSG_ERR, this->m_msg);
					retSt = GetInfiBlockVal(1, 1, 3, 3, 1751, &mastIntWeight);
					if(retSt != RT_NORMAL){
						_stprintf_s(this->m_msg, "Error reading Infi90 block (1,1,3,3,1751).");
						this->InsertMessage(MSG_ERR, this->m_msg);
						return;
					}

					retSt = VotedTagWrite(TWN_MM_INT_LOOSE, 0, mastIntWeight, FLT_VALUE);
					if(retSt != RT_NORMAL){
						_stprintf_s(this->m_msg, "Cannot write Main Mast intermediate loose of weight value.");
						this->InsertMessage(MSG_ERR, this->m_msg);
						return;
					}
				}
				else{
					retSt = VotedTagWrite(TWN_MM_INT_LOOSE, 0, mastIntWeight, FLT_VALUE);
					if(retSt != RT_NORMAL){
						//MessageBox("Cannot write Main Mast setpoint value",  _T("Error"), MB_OK | MB_ICONERROR);
						_stprintf_s(this->m_msg, "Cannot write Main Mast intermediate loose of weight value.");
						this->InsertMessage(MSG_ERR, this->m_msg);
						return;
					}
					retSt = VotedTagWrite(TWN_MM_TOT_LOOSE, 0, mastTotWeight, FLT_VALUE);
					if(retSt != RT_NORMAL){
						//MessageBox("Cannot write Main Mast setpoint value",  _T("Error"), MB_OK | MB_ICONERROR);
						_stprintf_s(this->m_msg, "Cannot write Main Mast total loose of weight quote value.");
						this->InsertMessage(MSG_ERR, this->m_msg);
						return;
					}
					
					retSt = VotedTagWrite(TWN_MM_TOT_LOOSE, 0, mastSp, FLT_VALUE);
					if(retSt != RT_NORMAL){
						//MessageBox("Cannot write Main Mast setpoint value",  _T("Error"), MB_OK | MB_ICONERROR);
						_stprintf_s(this->m_msg, "Cannot write Main Mast total loose of weight quote.");
						this->InsertMessage(MSG_ERR, this->m_msg);
						return;
					}
					
				}
			}
			*/
			clgrWeight = 0.0;
			if((this->m_mastSituation.obj[OBJT_N_CLUS] != 0) && (m_mastSituation.obj[OBJT_N_CLUS_CAS] != 0)){
				retSt = GetObjWeight(m_mastSituation.o_id[OBJT_N_CLUS], &objWeight);
				if(retSt != RT_NORMAL){
					_stprintf_s(this->m_msg, _T("Error retrieving object weight."));
					this->InsertMessage(MSG_ERR, this->m_msg);
					return;
				}
				clgrWeight = objWeight;
			}

			//Imposto i pesi, non considero gli oggetti ancora da prendere//
			weight = 0.0;
			objWeight = 0.0;
			for(j = 0; j < N_MAX_NONAM_OBJ; j++){
				if(this->m_mastSituation.obj[j] != 0){
					if(j < N_MAX_NAMED_OBJ){
						if(((j == OBJT_N_CLUS) && (m_mastSituation.obj[OBJT_N_CLUS_CAS] == 0)) || (j != OBJT_N_CLUS)){
							retSt = GetObjWeight(m_mastSituation.o_id[j], &objWeight);
							if(retSt != RT_NORMAL){
								_stprintf_s(this->m_msg, _T("Error setting object weight."));
								this->InsertMessage(MSG_ERR, this->m_msg);
								return;
							}
						}
					}
					else{
						retSt = GetObjtWeight(m_mastSituation.obj[j], &objWeight);
						if(retSt != RT_NORMAL){
							_stprintf_s(this->m_msg, _T("Error retrieving object type weight."));
							this->InsertMessage(MSG_ERR, this->m_msg);
							return;
						}
					}
					weight = weight + objWeight;
				}
			}
			retSt = VotedTagWrite(TWN_MM_EXP_WEIGHT, 0, weight, FLT_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error setting main mast expected weight."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;	
			}
			retSt = VotedTagWrite(TWN_MM_CL_EXP_WEIGHT, 0, clgrWeight, FLT_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error setting main mast cluster gripper expected weight."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			//Rilettura SP//
			prev = SetCursor(this->m_curHour);
			Sleep(SP_CHECK_INIT_WAIT);
			spOk = false;
			spCount = SP_CHECK_CYCLES;
			while(spCount > 0){
				Sleep(SP_CHECK_WAIT);

				retCnd = TagRead(&theApp.m_pDlg->m_tiMMSp, &tagVal);
				if(retCnd != R_NORMAL)
					spOk = false;
				else{
					if((tagVal.value.val_float < (float)(sp_1 - TOLL_MM)) || (tagVal.value.val_float > (float)(sp_1 + TOLL_MM)))
						spOk = false;
					else
						spOk = true;
				}

				spCount--;

				if(spOk)
					break;
			}

			prev = SetCursor(this->m_curNormal);

			if(!spOk){
				_stprintf_s(this->m_msg, _T("Error setting Main Mast set point value; try again please."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			SetMasterStation(local);

			retSt = VotedTagWrite(TWN_START_MOV, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write Start tag",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write Start tag."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			_stprintf_s(this->m_msg, _T("Main mast FA gripper movement started."));
			this->InsertMessage(MSG_OK, this->m_msg);

			Sleep(MAN_SLEEP_AFTER_START);

			break;
		case SP_OUT_OF_BOUND:
			//MessageBox("Main Mast set point inserted is out of bounds.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Inserted Main Mast set point is out of bounds."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
			break;
		case SP_IN_POS:
			//MessageBox("Main Mast is already is the selected position.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Main Mast is already is the selected position."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
			break;
		default:
			break;
		}		
		break;
	case MOV_MM_FIX:
		this->m_cmb_MMFix_sp.UpdateData(TRUE);
		if((!_tcscmp(this->m_MM_fix_pos, _T("OPEN"))) && (this->m_cmb_MMFix_sp.GetCurSel() == CMB_MM_FIX_OP)){
			//MessageBox("Main mast latch is already opened.",  _T("Error"), MB_OK | MB_ICONERROR);
			_stprintf_s(this->m_msg, _T("Main mast latch is already opened."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		if((!_tcscmp(this->m_MM_fix_pos, _T("CLOSED"))) && (this->m_cmb_MMFix_sp.GetCurSel() == CMB_MM_FIX_CL)){
			//MessageBox("Main mast latch is already opened.",  _T("Error"), MB_OK | MB_ICONERROR);
			_stprintf_s(this->m_msg, _T("Main mast latch is already closed."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
			
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMFixMreq, &tagVal);
		if(retCnd != R_NORMAL){
			//MessageBox("Cannot check Main Mast Latch movement request value",  _T("Error"), MB_OK | MB_ICONERROR);
			_stprintf_s(this->m_msg, _T("Cannot check Main Mast Latch movement request value."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		if(GtBit((short int)(tagVal.value.val_di), 15) == 0){
			//MessageBox("Main Mast Latch movement is not selected properly.",  _T("Error"), MB_OK | MB_ICONERROR);
			_stprintf_s(this->m_msg, _T("Main Mast Latch movement is not selected properly."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		if(this->m_cmb_MMFix_sp.GetCurSel() == CMB_MM_FIX_OP){
			retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast Latch open request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 0, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast Latch close request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
		}
		else if(this->m_cmb_MMFix_sp.GetCurSel() == CMB_MM_FIX_CL){
			retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast Latch close request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 0, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast Latch open request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
		}

		Sleep(200);

		SetMasterStation(local);

		retSt = VotedTagWrite(TWN_START_MOV, 1, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Cannot write Start tag."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		_stprintf_s(this->m_msg, _T("Main mast FA gripper latch movement started."));
		this->InsertMessage(MSG_OK, this->m_msg);

		Sleep(MAN_SLEEP_AFTER_START);
		break;
	case MOV_MM_CLUS:
		this->m_Clus_sp_c.GetWindowText(str);
		if(!_tcscmp(str, _T(""))){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Main mast cluster movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &sp_1);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Main mast cluster movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_MMClus_LL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast cluster Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &lowLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast cluster Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_MMClus_HL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast cluster Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &highLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast cluster Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = this->CheckSp(sp_1, this->m_Clus_pos, lowLim, highLim);
		switch(retInt){
		case SP_OK:
			retCnd = TagRead(&theApp.m_pDlg->m_tiMMClusMreq, &tagVal);
			if(retCnd != R_NORMAL){
				//MessageBox("Cannot check Main Mast cluster movement request value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot check Main Mast cluster movement request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			if(GtBit((short int)(tagVal.value.val_di), 15) == 0){
				//MessageBox("Main Mast cluster movement is not selected properly.",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Main Mast cluster movement is not selected properly."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			retSt = VotedTagWrite(TWN_CLGR_SP, 0, sp_1, FLT_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write Main Mast cluster setpoint value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast cluster setpoint value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			clgrWeight = 0.0;
			if((this->m_mastSituation.obj[OBJT_N_CLUS] != 0) && (m_mastSituation.obj[OBJT_N_CLUS_CAS] != 0)){
				retSt = GetObjWeight(m_mastSituation.o_id[OBJT_N_CLUS], &objWeight);
				if(retSt != RT_NORMAL){
					_stprintf_s(this->m_msg, _T("Error retrieving object weight."));
					this->InsertMessage(MSG_ERR, this->m_msg);
					return;
				}
				clgrWeight = objWeight;
			}

			//Imposto i pesi, non considero gli oggetti ancora da prendere//
			weight = 0.0;
			objWeight = 0.0;
			for(j = 0; j < N_MAX_NONAM_OBJ; j++){
				if(this->m_mastSituation.obj[j] != 0){
					if(j < N_MAX_NAMED_OBJ){
						if(((j == OBJT_N_CLUS) && (m_mastSituation.obj[OBJT_N_CLUS_CAS] == 0)) || (j != OBJT_N_CLUS)){
							retSt = GetObjWeight(m_mastSituation.o_id[j], &objWeight);
							if(retSt != RT_NORMAL){
								_stprintf_s(this->m_msg, _T("Error setting object weight."));
								this->InsertMessage(MSG_ERR, this->m_msg);
								return;
							}
						}
					}
					else{
						retSt = GetObjtWeight(m_mastSituation.obj[j], &objWeight);
						if(retSt != RT_NORMAL){
							_stprintf_s(this->m_msg, _T("Error retrieving object type weight."));
							this->InsertMessage(MSG_ERR, this->m_msg);
							return;
						}
					}
					weight = weight + objWeight;
				}
			}
			retSt = VotedTagWrite(TWN_MM_EXP_WEIGHT, 0, weight, FLT_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error setting main mast expected weight."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;	
			}
			retSt = VotedTagWrite(TWN_MM_CL_EXP_WEIGHT, 0, clgrWeight, FLT_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error setting main mast cluster gripper expected weight."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			//Rilettura SP//
			prev = SetCursor(this->m_curHour);
			Sleep(SP_CHECK_INIT_WAIT);
			spOk = false;
			spCount = SP_CHECK_CYCLES;
			while(spCount > 0){
				Sleep(SP_CHECK_WAIT);

				retCnd = TagRead(&theApp.m_pDlg->m_tiMMClusSp, &tagVal);
				if(retCnd != R_NORMAL)
					spOk = false;
				else{
					if((tagVal.value.val_float < (float)(sp_1 - TOLL_MM)) || (tagVal.value.val_float > (float)(sp_1 + TOLL_MM)))
						spOk = false;
					else
						spOk = true;
				}

				spCount--;

				if(spOk)
					break;
			}

			prev = SetCursor(this->m_curNormal);

			if(!spOk){
				_stprintf_s(this->m_msg, _T("Error setting Main Mast Cluster set point value; try again please."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			SetMasterStation(local);

			retSt = VotedTagWrite(TWN_START_MOV, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write Start tag",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write Start tag."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}	
		
			_stprintf_s(this->m_msg, _T("Main mast Cluster gripper latch movement started."));
			this->InsertMessage(MSG_OK, this->m_msg);

			Sleep(MAN_SLEEP_AFTER_START);
			break;	
		case SP_OUT_OF_BOUND:
			//MessageBox("Main Mast cluster set point inserted is out of bounds.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Main Mast cluster set point inserted is out of bounds."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
			break;
		case SP_IN_POS:
			//MessageBox("Main Mast cluster is already is the selected position.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Main Mast cluster is already is the selected position."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
			break;
		default:
			break;
		}
		break;
	case MOV_MM_ROT:
		this->m_MMR_sp_c.GetWindowText(str);
		if(!_tcscmp(str, _T(""))){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Main mast rotation movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &sp_1);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Main mast rotation movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_MMR_LL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast rotation Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &lowLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast rotation Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_MMR_HL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast rotation Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &highLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast rotation Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retInt = this->CheckSpMMRot(sp_1, this->m_MMR_pos, lowLim, highLim);
		switch(retInt){
		case SP_OK:
			retCnd = TagRead(&theApp.m_pDlg->m_tiMMRotMreq, &tagVal);
			if(retCnd != R_NORMAL){
				//MessageBox("Cannot check Main Mast rotation movement request value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot check Main Mast rotation movement request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			if(GtBit((short int)(tagVal.value.val_di), 15) == 0){
				//MessageBox("Main Mast rotation movement is not selected properly.",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Main Mast rotation movement is not selected properly."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			retSt = VotedTagWrite(TWN_MM_ROT_SP, 0, sp_1, FLT_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write Main Mast rotation setpoint value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast rotation setpoint value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			//Rilettura SP//
			prev = SetCursor(this->m_curHour);
			Sleep(SP_CHECK_INIT_WAIT);
			spOk = false;
			spCount = SP_CHECK_CYCLES;
			while(spCount > 0){
				Sleep(SP_CHECK_WAIT);

				retCnd = TagRead(&theApp.m_pDlg->m_tiMMRotSp, &tagVal);
				if(retCnd != R_NORMAL)
					spOk = false;
				else{
					if((tagVal.value.val_float < (float)(sp_1 - TOLL_MM)) || (tagVal.value.val_float > (float)(sp_1 + TOLL_MM)))
						spOk = false;
					else
						spOk = true;
				}

				spCount--;

				if(spOk)
					break;
			}

			prev = SetCursor(this->m_curNormal);

			if(!spOk){
				_stprintf_s(this->m_msg, _T("Error setting Main Mast Rotation set point value; try again please."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			SetMasterStation(local);

			retSt = VotedTagWrite(TWN_START_MOV, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write Start tag",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write Start tag."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}	

			_stprintf_s(this->m_msg, _T("Main mast rotation movement started."));
			this->InsertMessage(MSG_OK, this->m_msg);

			Sleep(MAN_SLEEP_AFTER_START);
			break;
		case SP_OUT_OF_BOUND:
			//MessageBox("Main Mast rotation set point inserted is out of bounds.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Main Mast rotation set point inserted is out of bounds."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
			break;
		case SP_IN_POS:
			//MessageBox("Main Mast rotation is already is the selected position.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Main Mast rotation is already is the selected position."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
			break;
		default:
			break;
		}
		break;
	case MOV_TV:

		this->m_TV_sp_c.GetWindowText(str);
		if(!_tcscmp(str, _T(""))){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for TV mast movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &sp_1);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for TV mast movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_TV_pos_LL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &lowLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_TV_pos_HL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &highLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = this->CheckSp(sp_1, this->m_TV_pos, lowLim, highLim);
		switch(retInt){
		case SP_OK:
			retCnd = TagRead(&theApp.m_pDlg->m_tiTVMreq, &tagVal);
			if(retCnd != R_NORMAL){
				//MessageBox("Cannot check TV mast movement request value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot check TV mast movement request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			if(GtBit((short int)(tagVal.value.val_di), 15) == 0){
				//MessageBox("TV mast movement is not selected properly.",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("TV mast movement is not selected properly."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			retSt = VotedTagWrite(TWN_TVM_SP, 0, sp_1, FLT_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write TV mast setpoint value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write TV mast setpoint value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			//Rilettura SP//
			prev = SetCursor(this->m_curHour);
			Sleep(SP_CHECK_INIT_WAIT);
			spOk = false;
			spCount = SP_CHECK_CYCLES;
			while(spCount > 0){
				Sleep(SP_CHECK_WAIT);

				retCnd = TagRead(&theApp.m_pDlg->m_tiTVSp, &tagVal);
				if(retCnd != R_NORMAL)
					spOk = false;
				else{
					if((tagVal.value.val_float < (float)(sp_1 - TOLL_MM)) || (tagVal.value.val_float > (float)(sp_1 + TOLL_MM)))
						spOk = false;
					else
						spOk = true;
				}

				spCount--;

				if(spOk)
					break;
			}

			prev = SetCursor(this->m_curNormal);

			if(!spOk){
				_stprintf_s(this->m_msg, _T("Error setting TV Mast set point value; try again please."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			SetMasterStation(local);

			retSt = VotedTagWrite(TWN_START_MOV, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write Start tag",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write Start tag."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}	

			_stprintf_s(this->m_msg, _T("TV mast movement started."));
			this->InsertMessage(MSG_OK, this->m_msg);

			Sleep(MAN_SLEEP_AFTER_START);
			break;
		case SP_OUT_OF_BOUND:
			//MessageBox("TV mast set point inserted is out of bounds.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("TV mast set point inserted is out of bounds."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
			break;
		case SP_IN_POS:
			//MessageBox("TV mast is already is the selected position.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("TV mast is already is the selected position."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
			break;
		default:
			break;
		}
		break;
	case MOV_TV_ROT:
		this->m_TVR_sp_c.GetWindowText(str);
		if(!_tcscmp(str, _T(""))){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for TV mast rotation movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &sp_1);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for TV mast rotation movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_TVR_LL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast rotation Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &lowLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast rotation Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_TVR_HL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast rotation Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &highLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast rotation Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retInt = this->CheckSp(sp_1, this->m_TVR_pos, lowLim, highLim);
		switch(retInt){
		case SP_OK:
			retCnd = TagRead(&theApp.m_pDlg->m_tiTVRotMreq, &tagVal);
			if(retCnd != R_NORMAL){
				//MessageBox("Cannot check TV mast rotation movement request value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot check TV mast rotation movement request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			if(GtBit((short int)(tagVal.value.val_di), 15) == 0){
				//MessageBox("TV mast rotation movement is not selected properly.",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("TV mast rotation movement is not selected properly."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			retSt = VotedTagWrite(TWN_TV_ROT_SP, 0, sp_1, FLT_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write TV mast rotation setpoint value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write TV mast rotation setpoint value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			//Rilettura SP//
			prev = SetCursor(this->m_curHour);
			Sleep(SP_CHECK_INIT_WAIT);
			spOk = false;
			spCount = SP_CHECK_CYCLES;
			while(spCount > 0){
				Sleep(SP_CHECK_WAIT);

				retCnd = TagRead(&theApp.m_pDlg->m_tiTVRotSp, &tagVal);
				if(retCnd != R_NORMAL)
					spOk = false;
				else{
					if((tagVal.value.val_float < (float)(sp_1 - TOLL_MM)) || (tagVal.value.val_float > (float)(sp_1 + TOLL_MM)))
						spOk = false;
					else
						spOk = true;
				}

				spCount--;

				if(spOk)
					break;
			}

			prev = SetCursor(this->m_curNormal);

			if(!spOk){
				_stprintf_s(this->m_msg, _T("Error setting TV Mast rotation set point value; try again please."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			SetMasterStation(local);

			retSt = VotedTagWrite(TWN_START_MOV, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write Start tag",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write Start tag."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}	
			
			_stprintf_s(this->m_msg, _T("TV mast rotation movement started."));
			this->InsertMessage(MSG_OK, this->m_msg);

			Sleep(MAN_SLEEP_AFTER_START);
			break;
		case SP_OUT_OF_BOUND:
			//MessageBox("TV mast rotation set point inserted is out of bounds.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("TV mast rotation set point inserted is out of bounds."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
			break;
		case SP_IN_POS:
			//MessageBox("TV mast rotation is already is the selected position.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("TV mast rotation is already is the selected position."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
			break;
		}
		break;
	case MOV_MM_DISPL:
		this->m_cmbMMDispl.UpdateData(TRUE);

		if((!_tcscmp(this->m_MMDisp_pos, _T("UP"))) && (this->m_cmbMMDispl.GetCurSel() == CMB_MM_DISPL_UP)){
			//MessageBox("Main mast latch is already opened.",  _T("Error"), MB_OK | MB_ICONERROR);
			_stprintf_s(this->m_msg, _T("Main mast displacing is already in upward position."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		if((!_tcscmp(this->m_MMDisp_pos, _T("DOWN"))) && (this->m_cmbMMDispl.GetCurSel() == CMB_MM_DISPL_DOWN)){
			//MessageBox("Main mast latch is already opened.",  _T("Error"), MB_OK | MB_ICONERROR);
			_stprintf_s(this->m_msg, _T("Main mast displacing is already in downward position."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
			
		retCnd = TagRead(&theApp.m_pDlg->m_tiMMDisplMreq, &tagVal);
		if(retCnd != R_NORMAL){
			//MessageBox("Cannot check Main Mast Latch movement request value",  _T("Error"), MB_OK | MB_ICONERROR);
			_stprintf_s(this->m_msg, _T("Cannot check Main mast displacing request."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		if(GtBit((short int)(tagVal.value.val_di), 15) == 0){
			//MessageBox("Main Mast Latch movement is not selected properly.",  _T("Error"), MB_OK | MB_ICONERROR);
			_stprintf_s(this->m_msg, _T("Main Mast Displacing movement is not selected properly."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		if(this->m_cmbMMDispl.GetCurSel() == CMB_MM_DISPL_UP){
			retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast Displacing upward request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 0, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast Displacing downward request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
		}
		else if(this->m_cmbMMDispl.GetCurSel() == CMB_MM_DISPL_DOWN){
			retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write Main Mast Latch close request value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast Displacing downward request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 0, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				//MessageBox("Cannot write Main Mast Latch open request value",  _T("Error"), MB_OK | MB_ICONERROR);
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast Displacing upward request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
		}

		Sleep(1000);

		SetMasterStation(local);

		retSt = VotedTagWrite(TWN_START_MOV, 1, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("Cannot write Start tag",  _T("Error"), MB_OK | MB_ICONERROR);
			_stprintf_s(this->m_msg, _T("Cannot write Start tag."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		_stprintf_s(this->m_msg, _T("Main mast Displacing movement started."));
		this->InsertMessage(MSG_OK, this->m_msg);

		Sleep(MAN_SLEEP_AFTER_START);
		
		break;	
	case MOV_FALV:
		retCnd = TagRead(&theApp.m_pDlg->m_tiFALVMreq, &tagVal);
		if(retCnd != R_NORMAL){
			//MessageBox("Cannot check Main Mast Latch movement request value",  _T("Error"), MB_OK | MB_ICONERROR);
			_stprintf_s(this->m_msg, _T("Cannot check FALVL movement request."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		if(GtBit((short int)(tagVal.value.val_di), 15) == 0){
			//MessageBox("Main Mast Latch movement is not selected properly.",  _T("Error"), MB_OK | MB_ICONERROR);
			_stprintf_s(this->m_msg, _T("FALV movement is not selected properly."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		if(this->m_cmbFALVL_sp.GetCurSel() == CMB_MM_FALVL_UP){
			retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write FALVL UP request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 0, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot reset FALVL DOWN request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
		}
		else if(this->m_cmbFALVL_sp.GetCurSel() == CMB_MM_FALVL_DOWN){
			retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write FALVL up request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 0, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot reset FALVL UP request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
		}

		Sleep(1000);

		SetMasterStation(local);

		retSt = VotedTagWrite(TWN_START_MOV, 1, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("Cannot write Start tag",  _T("Error"), MB_OK | MB_ICONERROR);
			_stprintf_s(this->m_msg, _T("Cannot write Start tag."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		_stprintf_s(this->m_msg, _T("FALV movement started."));
		this->InsertMessage(MSG_OK, this->m_msg);

		Sleep(MAN_SLEEP_AFTER_START);
		break;
	default:
		_stprintf_s(this->m_msg, _T("No movement selected."));
		this->InsertMessage(MSG_OK, this->m_msg);	
		break;
	}
}


bool ManualMode::LoadMreq(void){
	RETCOND			retCnd;
	TAGVALEX		tagVal;

	this->m_MovSelected = MOV_NULL;

	retCnd = TagRead(&theApp.m_pDlg->m_tiBridgeMreq, &tagVal);
	if(retCnd == R_NORMAL){
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){		
			this->m_Bridge_sp_c.EnableWindow(TRUE);
			this->m_MovSelected = MOV_BRIDGE;
			this->m_btnBridge.SetWindowText(_T("Unselect"));
		}
		else{
			this->m_Bridge_sp_c.EnableWindow(FALSE);
			this->m_btnBridge.SetWindowText(_T("Select"));
		}
	}
	
	retCnd = TagRead(&theApp.m_pDlg->m_tiTrolleyMreq, &tagVal);
	if(retCnd == R_NORMAL){
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			this->m_Trolley_sp_c.EnableWindow(TRUE);
			this->m_MovSelected = MOV_TROLLEY;
			this->m_btnTrolley.SetWindowText(_T("Unselect"));
		}
		else{
			this->m_Trolley_sp_c.EnableWindow(FALSE);
			this->m_btnTrolley.SetWindowText(_T("Select"));
		}
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiMMRotMreq, &tagVal);
	if(retCnd == R_NORMAL){
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			this->m_MovSelected = MOV_MM_ROT;
			this->m_btnMMRot.SetWindowText(_T("Unselect"));
			this->m_MMR_sp_c.EnableWindow(TRUE);
		}
		else{
			this->m_btnMMRot.SetWindowText(_T("Select"));
			this->m_MMR_sp_c.EnableWindow(FALSE);
		}
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiMMMreq, &tagVal);
	if(retCnd == R_NORMAL){
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			this->m_MovSelected = MOV_MM;
			this->m_btnMM.SetWindowText(_T("Unselect"));
			this->m_MM_sp_c.EnableWindow(TRUE);
		}
		else{
			this->m_btnMM.SetWindowText(_T("Select"));
			this->m_MM_sp_c.EnableWindow(FALSE);
		}
	}
	
	retCnd = TagRead(&theApp.m_pDlg->m_tiMMFixMreq, &tagVal);
	if(retCnd == R_NORMAL){
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			this->m_MovSelected = MOV_MM_FIX;
			this->m_btnMMFix.SetWindowText(_T("Unselect"));
			this->m_cmb_MMFix_sp.EnableWindow(TRUE);
		}
		else{
			this->m_btnMMFix.SetWindowText(_T("Select"));
			this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		}
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiMMClusMreq, &tagVal);
	if(retCnd == R_NORMAL){
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			this->m_MovSelected = MOV_MM_CLUS;
			this->m_btnMMClus.SetWindowText(_T("Unselect"));
			this->m_Clus_sp_c.EnableWindow(TRUE);
		}
		else{
			this->m_btnMMClus.SetWindowText(_T("Select"));
			this->m_Clus_sp_c.EnableWindow(FALSE);
		}
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiTVRotMreq, &tagVal);
	if(retCnd == R_NORMAL){
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			this->m_MovSelected = MOV_TV_ROT;
			this->m_btnTVRot.SetWindowText(_T("Unselect"));
			this->m_TVR_sp_c.EnableWindow(TRUE);
		}
		else{
			this->m_btnTVRot.SetWindowText(_T("Select"));
			this->m_TVR_sp_c.EnableWindow(FALSE);
		}
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiTVMreq, &tagVal);
	if(retCnd == R_NORMAL){
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			this->m_MovSelected = MOV_TV;
			this->m_btnTV.SetWindowText(_T("Unselect"));
			this->m_TV_sp_c.EnableWindow(TRUE);
		}
		else{
			this->m_btnTV.SetWindowText(_T("Select"));
			this->m_TV_sp_c.EnableWindow(FALSE);
		}
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiMMDisplMreq, &tagVal);
	if(retCnd == R_NORMAL){
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			this->m_MovSelected = MOV_MM_DISPL;
			this->m_btnMMDispl.SetWindowText(_T("Unselect"));
			this->m_cmbMMDispl.EnableWindow(TRUE);
		}
		else{
			this->m_btnMMDispl.SetWindowText(_T("Select"));
			this->m_cmbMMDispl.EnableWindow(FALSE);
		}
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiFALVMreq, &tagVal);
	if(retCnd == R_NORMAL){
		if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
			this->m_MovSelected = MOV_FALV;
			this->m_btnFALVL.SetWindowText(_T("Unselect"));
			this->m_cmbFALVL_sp.EnableWindow(FALSE);
		}
		else{
			this->m_btnFALVL.SetWindowText(_T("Select"));
			this->m_cmbFALVL_sp.EnableWindow(FALSE);
		}
	}

	this->UpdateData(false);

	return(true);
}

int	ManualMode::CheckSp(float sp, float pos, float ll, float hl){
	float		diff;

	diff = sp - pos;
	if(diff < 0)
		diff = (float)(diff * (-1));
	if(diff <= (float)(TOLL_SP_IN_POS))
		return(SP_IN_POS);

	if((sp < (float)(ll - TOLL_SP_RANGE)) || (sp > (float)(hl + TOLL_SP_RANGE)))
		return(SP_OUT_OF_BOUND);

	return(SP_OK);		
}

int	ManualMode::CheckSpMMRot(float sp, float pos, float ll, float hl){
	float		diff;

	diff = sp - pos;
	if(diff < 0)
		diff = (float)(diff * (-1));
	if(diff < (float)(0.1))
		return(SP_IN_POS);

	if((sp < (float)(ll - TOLL_SP_RANGE)) || (sp > (float)(hl + TOLL_SP_RANGE)))
		return(SP_OUT_OF_BOUND);

	return(SP_OK);		
}

void ManualMode::OnStop(){
	RETSTAT			retSt;

	if(this->m_MovInProg){
		retSt = VotedTagWrite(TWN_START_MOV, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("Movement could not be stopped",  _T("Error"), MB_OK | MB_ICONERROR);
			_stprintf_s(this->m_msg, _T("Movement could not be stopped."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		_stprintf_s(this->m_msg, _T("Movement stopped."));
		this->InsertMessage(MSG_OK, this->m_msg);
		if((this->m_loggedUser <= 4 && this->m_loggedUser > 0) || this->m_loggedUser == SYSMGR)
			this->m_btnExe.EnableWindow(true);
		this->m_btnStop.EnableWindow(false);
	}
	else{
		//MessageBox("No movement in progress", "Warning", MB_OK | MB_ICONINFORMATION);
		_stprintf_s(this->m_msg, _T("No movement in progress."));
		this->InsertMessage(MSG_WRN, this->m_msg);
	}
}

void ManualMode::OnExit(){
	if(this->m_MovInProg){
		//MessageBox("Cannot exit, a movement is in progress.", "Warning", MB_OK | MB_ICONERROR);
		_stprintf_s(this->m_msg, _T("Cannot exit, a movement is in progress."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	theApp.m_pDlg->m_ManModeOpened = false;
	CDialog::OnCancel();
}


void ManualMode::InsertMessage(int type, TCHAR msg[MAN_MSG_LEN]){
	SYSTEMTIME		sysTime;

	switch(type){
	case MSG_OK:
		this->m_txtStatus.Format(_T("OK"));
		this->m_msgIcon.SetIcon(this->m_iconOK);
		break;
	case MSG_WRN:
		this->m_txtStatus.Format(_T("WRN"));
		this->m_msgIcon.SetIcon(this->m_iconWRN);
		break;
	case MSG_ERR:
		this->m_txtStatus.Format(_T("ERR"));
		this->m_msgIcon.SetIcon(this->m_iconERR);
		break;
	default:
		this->m_txtStatus.Format(_T("UNK"));
		this->m_msgIcon.SetIcon(NULL);
		break;
	}

	this->m_txtDescr.Format(_T("%s"), msg);

	GetLocalTime(&sysTime);

	this->m_txtDate.Format(_T("%02d/%02d/%d %02d:%02d.%02d"), sysTime.wDay, sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond);

	//this->_UpdateData(UD_ECHO_FALSE);
	this->UpdateData(false);
}

void ManualMode::IsReady(void){
	SYSTEMTIME		sysTime;

	this->m_txtStatus.Format(_T("OK"));
	this->m_msgIcon.SetIcon(theApp.LoadIcon(IDI_ICON_MSG_OK));
	this->m_txtDescr.Format(_T("Ready"));
	GetLocalTime(&sysTime);
	this->m_txtDate.Format(_T("%02d/%02d/%d %02d:%02d.%02d"), sysTime.wDay, sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond);
	//this->_UpdateData(UD_ECHO_FALSE);
	this->UpdateData(false);
}

void ManualMode::OnAck(){
	this->IsReady();
}

bool ManualMode::LoadParam(){
	RETSTAT			retSt;
	GENIDX			zoneIdx;
	ZONENAME		zoneName;
	float			px;
	float			py;
	TCHAR			userName[SZUNAM+1];
	int				mode;
	int				vx;
	int				vy;
	int				flag;

	//USER
	retSt = GetLoggedUserName(theApp.m_pDlg->m_opStation, userName);
	if(retSt == RT_NORMAL)
		this->m_txtUser.Format(_T("%s"), userName);
	else
		this->m_txtUser.Format(_T("Unknown"));

	//OP MODE
	retSt = GetDynCurrentMode(&mode);
	if(retSt == RT_NORMAL){
		switch(mode){
		case M_NO_MODE:
			this->m_txtOpMode.Format(_T("No mode"));
			break;
		case M_AUTOMATIC:
			this->m_txtOpMode.Format(_T("Automatic"));
			break;
		case M_SEMIAUTO:
			this->m_txtOpMode.Format(_T("Semi-Automatic"));
			break;
		case M_PROTECT:
			this->m_txtOpMode.Format(_T("Protected Manual"));
			break;
		case M_MAN_SINGLE_CH:
			this->m_txtOpMode.Format(_T("Manual single Channel"));
			break;
		case M_MAN_WO_INT:
			this->m_txtOpMode.Format(_T("Manual without Interlocks"));
			break;
		default:
			this->m_txtOpMode.Format(_T("Unknown mode"));
			break;
		}
	}
	else
		this->m_txtOpMode.Format(_T("Unknown mode"));

	//Zone
	retSt = GetMastPosInfiSC(this->m_refMast, &px, &py, &zoneIdx);
	if(retSt == RT_NORMAL){
		retSt = GetZoneName(zoneIdx, zoneName);
		if(retSt == RT_NORMAL)
			this->m_txtZone.Format(_T("%s"), zoneName);
		else
			this->m_txtZone.Format(_T("Unknown"));
	}
	else
		this->m_txtZone.Format(_T("Unknown"));
	
	retSt = GetZonePhisVirtCoord(px, py, &vx, &vy);
	if(retSt != RT_NORMAL){
		this->m_txtVirtCoo.Format(_T("(---,---)"));
		this->m_vx = 0;
		this->m_vy = 0;
		this->m_onCell = false;
	}
	else{
		this->m_txtVirtCoo.Format(_T("(%d,%d)"), vx, vy);
		this->m_vx = vx;
		this->m_vy = vy;
		this->m_onCell = true;
	}

	retSt = sysInSimulation(&flag);
	if(retSt != RT_NORMAL)
		this->m_txtZone.Format(_T("ERR"));
	else{
		if(flag == 1)
			this->m_sysStatus.Format(_T("SIMULATION"));
		else
			this->m_sysStatus.Format(_T("CONTROL"));
	}

	this->_UpdateData(false);

	return(true);
}

bool ManualMode::LoadMast(){
	RETSTAT			retSt;
	TCHAR			objtDescr[128];
	OBJNAME			objName;
	int				i;
	int				count;

	count = -1;

	ZeroMemory(&this->m_mastSituation, sizeof(OBJTMM));
	retSt = GetMastSituation(&this->m_mastSituation);
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("Error retrieving Main mast current situation."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return(false);
	}

	this->m_lstMast.DeleteAllItems();

	for(i = 0; i < N_MAX_NONAM_OBJ; i++){
		if(this->m_mastSituation.obj[i] != 0){
			count++;
			retSt = GetObjtDescription(this->m_mastSituation.obj[i], objtDescr);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error retrieving object type name for Idx %d."), this->m_mastSituation.obj[i]);
				this->InsertMessage(MSG_ERR, this->m_msg);
			}
		
			if(i < N_MAX_NAMED_OBJ){
				if(this->m_mastSituation.o_id[i] != 0){
					retSt = GetObjName(this->m_mastSituation.o_id[i], objName);
					if(retSt != RT_NORMAL){
						_stprintf_s(this->m_msg, _T("Error retrieving object name for Idx %d."), this->m_mastSituation.o_id[i]);
						this->InsertMessage(MSG_ERR, this->m_msg);
					}
				}
			}
			else
				_stprintf_s(objName, _T("---"));
			this->m_lstMast.InsertItem(LVIF_TEXT, count, objtDescr, NULL, NULL, NULL, NULL);
			this->m_lstMast.SetItemText(count, 1, objName);
		}
	}

	return(true);
}

bool ManualMode::LoadCell(){
	RETSTAT			retSt;
	TCHAR			objtDescr[128];
	OBJNAME			objName;
	int				i;
	int				count;

	count = -1;
	
	if(!this->m_onCell){
		if(this->m_refMast == MAIN_MAST)
			this->m_cellInfo.Format(_T("Main Mast is not on cell"));
		else if(this->m_refMast == FALC_MAST)
			this->m_cellInfo.Format(_T("FALVL is not on cell"));
		else if(this->m_refMast == DMP_MAST)
			this->m_cellInfo.Format(_T("DIMP is not on cell"));
		this->m_lstCell.DeleteAllItems();
		return(true);
	}

	this->m_cellInfo.Format(_T("Cell (%d,%d) situation"), this->m_vx, this->m_vy);
	ZeroMemory(&this->m_cellSituation, sizeof(OBJTCELL));
	retSt = GetCellSituation(this->m_vx, this->m_vy, &this->m_cellSituation);
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("Error retrieving cell (%d,%d) current situation."), this->m_vx, this->m_vy);
		this->InsertMessage(MSG_ERR, this->m_msg);
		return(false);
	}

	this->m_lstCell.DeleteAllItems();

	for(i = 0; i < N_MAX_NONAM_OBJ; i++){
		if(this->m_cellSituation.obj[i] != 0){
			count++;
			retSt = GetObjtDescription(this->m_cellSituation.obj[i], objtDescr);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error retrieving object type name for Idx %d."), this->m_cellSituation.obj[i]);
				this->InsertMessage(MSG_ERR, this->m_msg);
			}
		
			if(i < N_MAX_NAMED_OBJ){
				if(this->m_cellSituation.o_id[i] != 0){
					retSt = GetObjName(this->m_cellSituation.o_id[i], objName);
					if(retSt != RT_NORMAL){
						_stprintf_s(this->m_msg, _T("Error retrieving object name for Idx %d."), this->m_cellSituation.o_id[i]);
						this->InsertMessage(MSG_ERR, this->m_msg);
					}
				}
			}
			else
				_stprintf_s(objName, _T("---"));
			this->m_lstCell.InsertItem(LVIF_TEXT, count, objtDescr, NULL, NULL, NULL, NULL);
			this->m_lstCell.SetItemText(count, 1, objName);
		}
	}

	return(true);
}

void ManualMode::_UpdateData(int updateType){
	switch(updateType){
	case UD_TOP_FALSE:
		this->m_txtOpMode_c.UpdateData(false);
		this->m_txtUser_c.UpdateData(false);
		this->m_txtVirtCoo_c.UpdateData(false);
		this->m_txtZone_c.UpdateData(false);
		this->m_cellInfo_c.UpdateData(false);
		break;
	case UD_DOWN_FALSE:
		this->m_Bridge_pos_c.UpdateData(false);
		this->m_Trolley_pos_c.UpdateData(false);
		this->m_MM_pos_c.UpdateData(false);
		this->m_MMR_pos_c.UpdateData(false);
		this->m_Clus_pos_c.UpdateData(false);
		this->m_TV_pos_c.UpdateData(false);
		this->m_TVR_pos_c.UpdateData(false);
		this->m_MM_fix_pos_c.UpdateData(false);

		this->m_Bridge_pos_HL_c.UpdateData(false);
		this->m_Trolley_pos_HL_c.UpdateData(false);
		this->m_MM_pos_HL_c.UpdateData(false);
		this->m_MMR_HL_c.UpdateData(false);
		this->m_MMClus_HL_c.UpdateData(false);
		this->m_TV_pos_HL_c.UpdateData(false);
		this->m_TVR_HL_c.UpdateData(false);
		
		this->m_Bridge_pos_LL_c.UpdateData(false);
		this->m_Trolley_pos_LL_c.UpdateData(false);
		this->m_MM_pos_LL_c.UpdateData(false);
		this->m_MMR_LL_c.UpdateData(false);
		this->m_MMClus_LL_c.UpdateData(false);
		this->m_TV_pos_LL_c.UpdateData(false);
		this->m_TVR_LL_c.UpdateData(false);

		this->m_Bridge_sp_rc.UpdateData(false);
		this->m_Trolley_sp_rc.UpdateData(false);
		this->m_MM_sp_rc.UpdateData(false);
		this->m_MMR_sp_rc.UpdateData(false);
		this->m_Clus_sp_rc.UpdateData(false);
		this->m_TV_sp_rc.UpdateData(false);
		this->m_TVR_sp_rc.UpdateData(false);
	
		break;
	case UD_LISTS_FALSE:
		this->m_lstCell.UpdateData(false);
		this->m_lstMast.UpdateData(false);
		break;
	case UD_ECHO_FALSE:
		this->m_txtDate_c.UpdateData(false);
		this->m_msgIcon.UpdateData(false);
		this->m_txtStatus_c.UpdateData(false);
		this->m_txtDescr_c.UpdateData(false);
		break;
	case UD_TOP_TRUE:
		this->m_txtOpMode_c.UpdateData(true);
		this->m_txtUser_c.UpdateData(true);
		this->m_txtVirtCoo_c.UpdateData(true);
		this->m_txtZone_c.UpdateData(true);
		this->m_cellInfo_c.UpdateData(true);
		break;
	case UD_DOWN_TRUE:
		this->m_Bridge_pos_c.UpdateData(true);
		this->m_Trolley_pos_c.UpdateData(true);
		this->m_MM_pos_c.UpdateData(true);
		this->m_MMR_pos_c.UpdateData(true);
		this->m_Clus_pos_c.UpdateData(true);
		this->m_TV_pos_c.UpdateData(true);
		this->m_TVR_pos_c.UpdateData(true);
		this->m_MM_fix_pos_c.UpdateData(true);

		this->m_Bridge_pos_HL_c.UpdateData(true);
		this->m_Trolley_pos_HL_c.UpdateData(true);
		this->m_MM_pos_HL_c.UpdateData(true);
		this->m_MMR_HL_c.UpdateData(true);
		this->m_MMClus_HL_c.UpdateData(true);
		this->m_TV_pos_HL_c.UpdateData(true);
		this->m_TVR_HL_c.UpdateData(true);
		
		this->m_Bridge_pos_LL_c.UpdateData(true);
		this->m_Trolley_pos_LL_c.UpdateData(true);
		this->m_MM_pos_LL_c.UpdateData(true);
		this->m_MMR_LL_c.UpdateData(true);
		this->m_MMClus_LL_c.UpdateData(true);
		this->m_TV_pos_LL_c.UpdateData(true);
		this->m_TVR_LL_c.UpdateData(true);

		this->m_Bridge_sp_rc.UpdateData(true);
		this->m_Trolley_sp_rc.UpdateData(true);
		this->m_MM_sp_rc.UpdateData(true);
		this->m_MMR_sp_rc.UpdateData(true);
		this->m_Clus_sp_rc.UpdateData(true);
		this->m_TV_sp_rc.UpdateData(true);
		this->m_TVR_sp_rc.UpdateData(true);
	
		break;
	case UD_LISTS_TRUE:
		this->m_lstCell.UpdateData(true);
		this->m_lstMast.UpdateData(true);
		break;
	case UD_ECHO_TRUE:
		this->m_txtDate_c.UpdateData(true);
		this->m_msgIcon.UpdateData(true);
		this->m_txtStatus_c.UpdateData(true);
		this->m_txtDescr_c.UpdateData(true);
		break;
	default:
		break;
	}
}

bool ManualMode::CheckTrip(){
	RETSTAT		retSt;
	int			flag;
	TCHAR		descr[TRIP_DESCR_LEN];
	bool		ret;

	retSt = GetTripInfo(&flag, descr);
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("Error checking FHM trip status."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return(false);
	}
	switch(flag){
	case TRIP_OFF:
		ret = false;
		break;
	case TRIP_ALL:
		_stprintf_s(this->m_msg, _T("%s"), descr);
		this->InsertMessage(MSG_ERR, this->m_msg);
		ret = true;
		break;
	case TRIP_BRIDGE:
		_stprintf_s(this->m_msg, _T("Bridge trip: %s"), descr);
		this->InsertMessage(MSG_ERR, this->m_msg);
		ret = true;
		break;
	case TRIP_TROLLEY:
		_stprintf_s(this->m_msg, _T("Trolley trip: %s"), descr);
		this->InsertMessage(MSG_ERR, this->m_msg);
		ret = true;
		break;
	case TRIP_MM:
		_stprintf_s(this->m_msg, _T("Main mast trip: %s"), descr);
		this->InsertMessage(MSG_ERR, this->m_msg);
		ret = true;
		break;
	case TRIP_MM_CLUS:
		_stprintf_s(this->m_msg, _T("Main mast cluster trip: %s"), descr);
		this->InsertMessage(MSG_ERR, this->m_msg);
		ret = true;
		break;
	case TRIP_MM_ROT:
		_stprintf_s(this->m_msg, _T("Main mast rotation trip: %s"), descr);
		this->InsertMessage(MSG_ERR, this->m_msg);
		ret = true;
		break;
	case TRIP_TV:
		_stprintf_s(this->m_msg, _T("TV mast trip: %s"), descr);
		this->InsertMessage(MSG_ERR, this->m_msg);
		ret = true;
		break;
	case TRIP_TV_ROT:
		_stprintf_s(this->m_msg, _T("TV mast rotation trip: %s"), descr);
		this->InsertMessage(MSG_ERR, this->m_msg);
		ret = true;
		break;
	case TRIP_MM_DISPL:
		_stprintf_s(this->m_msg, _T("Main mast displacing trip: %s"), descr);
		this->InsertMessage(MSG_ERR, this->m_msg);
		ret = true;
		break;
	case TRIP_FALV:
		_stprintf_s(this->m_msg, _T("FALV trip: %s"), descr);
		this->InsertMessage(MSG_ERR, this->m_msg);
		ret = true;
		break;
	default:
		ret = false;
		break;
	}

	return(ret);
}

void ManualMode::OnResetTrip(){
	RETSTAT		retSt;

	retSt = VotedTagWrite(TWN_TRIP_RESET, 1, 0, DIG_VALUE);
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("Error writing Trip reset Tag value."));
		this->InsertMessage(MSG_ERR, this->m_msg);
	}
	else{
		_stprintf_s(this->m_msg, _T("Movement trip successfully reset."));
		this->InsertMessage(MSG_OK, this->m_msg);
	}

	//this->m_MovSelected = MOV_NULL;

	//this->UnselectMov();
}

void ManualMode::UnselectMov(){
	RETSTAT		retSt;

	if(this->m_MovSelected != MOV_NULL){
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);

		retSt = VotedTagWrite(TWN_BRIDGE_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("Bridge movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Bridge movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TROLLEY_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("Trolley movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Trolley movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("FA gripper latch movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("FA gripper request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("FA gripper latch movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("FA gripper latch movement FIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("FA gripper latch movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("FA gripper latch movement UNFIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("FA gripper latch movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("FA gripper latch movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_CLGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("Cluster gripper movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Cluster gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_MM_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("MM rotation movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("MM rotation  movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_TVM_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("TV mast movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("TV mast movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TV_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("TV mast rotation request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("TV mast rotation movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("TV mast rotation request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("MM Displacing movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_FALV_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_MovSelected = MOV_NULL;
		
		this->UpdateData(false);
	}
}

void ManualMode::OnBtnDbop(){
	GENIDX		zoneIdx;

	if(theApp.m_pDlg->m_pDB_op == NULL){
		theApp.m_pDlg->m_pDB_op = new CDB_op;
		if(theApp.m_pDlg->m_pDB_op != NULL){
			theApp.m_pDlg->m_pDB_op->m_defVx = 21101;
			theApp.m_pDlg->m_pDB_op->m_defVy = 21102;
			GetZoneIdx(_T("Z211"), &zoneIdx);
			theApp.m_pDlg->m_pDB_op->m_defZoneIdx = zoneIdx;
			theApp.m_pDlg->m_pDB_op->Create(IDD_DB_OP, NULL);
			theApp.m_pDlg->m_pDB_op->ShowWindow(SW_SHOW);
		}
		else
			MessageBox(_T("Error inizializing window pointer."),  _T("Error"), MB_OK|MB_ICONERROR);
	}
	else
		theApp.m_pDlg->m_pDB_op->ShowWindow(SW_SHOW);
	
}

LRESULT ManualMode::OnMastChanged(WPARAM wParam, LPARAM lParam ){
	RETSTAT			retSt;
	TCHAR			objtDescr[128];
	OBJNAME			objName;
	int				i;
	int				count;

	count = -1;

	ZeroMemory(&this->m_mastSituation, sizeof(OBJTMM));
	retSt = GetMastSituation(&this->m_mastSituation);
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("Error retrieving Main mast current situation."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return 0;
	}

	this->m_lstMast.DeleteAllItems();

	for(i = 0; i < N_MAX_NONAM_OBJ; i++){
		if(this->m_mastSituation.obj[i] != 0){
			count++;
			retSt = GetObjtDescription(this->m_mastSituation.obj[i], objtDescr);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error retrieving object type name for Idx %d."), this->m_mastSituation.obj[i]);
				this->InsertMessage(MSG_ERR, this->m_msg);
			}
		
			if(i < N_MAX_NAMED_OBJ){
				if(this->m_mastSituation.o_id[i] != 0){
					retSt = GetObjName(this->m_mastSituation.o_id[i], objName);
					if(retSt != RT_NORMAL){
						_stprintf_s(this->m_msg, _T("Error retrieving object name for Idx %d."), this->m_mastSituation.o_id[i]);
						this->InsertMessage(MSG_ERR, this->m_msg);
					}
				}
			}
			else
				_stprintf_s(objName, _T("---"));
			this->m_lstMast.InsertItem(LVIF_TEXT, count, objtDescr, NULL, NULL, NULL, NULL);
			this->m_lstMast.SetItemText(count, 1, objName);
		}
	}

	return 0;
}

LRESULT ManualMode::OnCellChanged(WPARAM wParam, LPARAM lParam ){
	RETSTAT			retSt;
	TCHAR			objtDescr[128];
	OBJNAME			objName;
	int				i;
	int				count;
	int				vx;
	int				vy;

	count = -1;
	
	if(!this->m_onCell){
		this->m_lstCell.DeleteAllItems();
		if(this->m_refMast == MAIN_MAST)
			this->m_cellInfo.Format(_T("Main Mast is not on cell"));
		else if(this->m_refMast == FALC_MAST)
			this->m_cellInfo.Format(_T("FALVL is not on cell"));
		else if(this->m_refMast == DMP_MAST)
			this->m_cellInfo.Format(_T("DIMP is not on cell"));
		this->UpdateData(false);
		return 0;
	}
	
	vx = wParam;
	vy = lParam;

	this->m_cellInfo.Format(_T("Cell (%d,%d) situation"), vx, vy);
	ZeroMemory(&this->m_cellSituation, sizeof(OBJTCELL));
	retSt = GetCellSituation(vx, vy, &this->m_cellSituation);
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("Error retrieving cell (%d,%d) current situation."), vx, vy);
		this->InsertMessage(MSG_ERR, this->m_msg);
		return 0;
	}

	this->m_lstCell.DeleteAllItems();

	for(i = 0; i < N_MAX_NONAM_OBJ; i++){
		if(this->m_cellSituation.obj[i] != 0){
			count++;
			retSt = GetObjtDescription(this->m_cellSituation.obj[i], objtDescr);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error retrieving object type name for Idx %d."), this->m_cellSituation.obj[i]);
				this->InsertMessage(MSG_ERR, this->m_msg);
			}
		
			if(i < N_MAX_NAMED_OBJ){
				if(this->m_cellSituation.o_id[i] != 0){
					retSt = GetObjName(this->m_cellSituation.o_id[i], objName);
					if(retSt != RT_NORMAL){
						_stprintf_s(this->m_msg, _T("Error retrieving object name for Idx %d."), this->m_cellSituation.o_id[i]);
						this->InsertMessage(MSG_ERR, this->m_msg);
					}
				}
			}
			else
				_stprintf_s(objName, _T("---"));
			this->m_lstCell.InsertItem(LVIF_TEXT, count, objtDescr, NULL, NULL, NULL, NULL);
			this->m_lstCell.SetItemText(count, 1, objName);
		}
	}
	this->UpdateData(false);
	return 0;
}

LRESULT ManualMode::OnSpChanged(WPARAM wParam, LPARAM lParam ){
	RETSTAT		retSt;
	float		llim;
	float		hlim;
	TCHAR		text[64];

	//BRIDGE LIMITS//
	retSt = GetTagLimits(theApp.m_pDlg->m_tiBridgeSp, &llim, &hlim);
	if(retSt == RT_NORMAL){
		_stprintf_s(text, _T("%.1f"), llim);
		this->m_Bridge_pos_LL_c.SetWindowText(text);
		_stprintf_s(text, _T("%.1f"), hlim);
		this->m_Bridge_pos_HL_c.SetWindowText(text);
	}
	else{
		this->m_Bridge_pos_LL_c.SetWindowText(_T("ERR"));
		this->m_Bridge_pos_HL_c.SetWindowText(_T("ERR"));
	}
		
	//TROLLEY LIMITS//
	retSt = GetTagLimits(theApp.m_pDlg->m_tiTrolleySp, &llim, &hlim);
	if(retSt == RT_NORMAL){
		_stprintf_s(text, _T("%.1f"), llim);
		this->m_Trolley_pos_LL_c.SetWindowText(text);
		_stprintf_s(text, _T("%.1f"), hlim);
		this->m_Trolley_pos_HL_c.SetWindowText(text);
	}
	else{
		this->m_Trolley_pos_LL_c.SetWindowText(_T("ERR"));
		this->m_Trolley_pos_HL_c.SetWindowText(_T("ERR"));
	}

	//TVR LIMITS//
	retSt = GetTagLimits(theApp.m_pDlg->m_tiTVRotSp, &llim, &hlim);
	if(retSt == RT_NORMAL){
		_stprintf_s(text, _T("%.1f"), llim);
		this->m_TVR_LL_c.SetWindowText(text);
		_stprintf_s(text, _T("%.1f"), hlim);
		this->m_TVR_HL_c.SetWindowText(text);
	}
	else{
		this->m_TVR_LL_c.SetWindowText(_T("ERR"));
		this->m_TVR_HL_c.SetWindowText(_T("ERR"));
	}

	//MM LIMITS//
	retSt = GetTagLimits(theApp.m_pDlg->m_tiMMSp, &llim, &hlim);
	if(retSt == RT_NORMAL){
		_stprintf_s(text, _T("%.1f"), llim);
		this->m_MM_pos_LL_c.SetWindowText(text);
		_stprintf_s(text, _T("%.1f"), hlim);
		this->m_MM_pos_HL_c.SetWindowText(text);
	}
	else{
		this->m_MM_pos_LL_c.SetWindowText(_T("ERR"));
		this->m_MM_pos_HL_c.SetWindowText(_T("ERR"));
	}

	//TVM LIMITS//
	retSt = GetTagLimits(theApp.m_pDlg->m_tiTVSp, &llim, &hlim);
	if(retSt == RT_NORMAL){
		_stprintf_s(text, _T("%.1f"), llim);
		this->m_TV_pos_LL_c.SetWindowText(text);
		_stprintf_s(text, _T("%.1f"), hlim);
		this->m_TV_pos_HL_c.SetWindowText(text);
	}
	else{
		this->m_TV_pos_LL_c.SetWindowText(_T("ERR"));
		this->m_TV_pos_HL_c.SetWindowText(_T("ERR"));
	}

	//MM CLUS LIMITS//
	retSt = GetTagLimits(theApp.m_pDlg->m_tiMMClusSp, &llim, &hlim);
	if(retSt == RT_NORMAL){
		_stprintf_s(text, _T("%.1f"), llim);
		this->m_MMClus_LL_c.SetWindowText(text);
		_stprintf_s(text, _T("%.1f"), hlim);
		this->m_MMClus_HL_c.SetWindowText(text);
	}
	else{
		this->m_MMClus_LL_c.SetWindowText(_T("ERR"));
		this->m_MMClus_HL_c.SetWindowText(_T("ERR"));
	}

	//MM ROTATION LIMITS//
	retSt = GetTagLimits(theApp.m_pDlg->m_tiMMRotSp, &llim, &hlim);
	if(retSt == RT_NORMAL){
		_stprintf_s(text, _T("%.1f"), hlim);
		this->m_MMR_HL_c.SetWindowText(text);
		_stprintf_s(text, _T("%.1f"), llim);
		this->m_MMR_LL_c.SetWindowText(text);
	}
	else{
		this->m_MMR_HL_c.SetWindowText(_T("ERR"));
		this->m_MMR_LL_c.SetWindowText(_T("ERR"));
	}

	return 0;
}

bool ManualMode::LoadLimits(void){
	/*
	RETSTAT			retSt;
	float			llim;
	float			hlim;	
	char			text[64];
	
	//BRIDGE LIMITS//
	retSt = GetTagLimits(theApp.m_pDlg->m_tiBridgeSp, &llim, &hlim);
	if(retSt == RT_NORMAL){
		_stprintf_s(text, "%.0f", llim);
		this->m_Bridge_pos_LL_c.SetWindowText(text);
		this->m_Bridge_pos_LL = llim;
		_stprintf_s(text, "%.0f", hlim);
		this->m_Bridge_pos_HL_c.SetWindowText(text);
		this->m_Bridge_pos_HL = hlim;
	}
	else{
		this->m_Bridge_pos_LL_c.SetWindowText("ERR");
		this->m_Bridge_pos_HL_c.SetWindowText("ERR");
		this->m_Bridge_pos_LL = (float)(-1);
		this->m_Bridge_pos_HL = (float)(-1);
	}
		
	//TROLLEY LIMITS//
	retSt = GetTagLimits(theApp.m_pDlg->m_tiTrolleySp, &llim, &hlim);
	if(retSt == RT_NORMAL){
		_stprintf_s(text, "%.0f", llim);
		this->m_Trolley_pos_LL_c.SetWindowText(text);
		this->m_Trolley_pos_LL = llim;
		_stprintf_s(text, "%.0f", hlim);
		this->m_Trolley_pos_HL_c.SetWindowText(text);
		this->m_Trolley_pos_HL = hlim;
	}
	else{
		this->m_Trolley_pos_LL_c.SetWindowText("ERR");
		this->m_Trolley_pos_HL_c.SetWindowText("ERR");
		this->m_Trolley_pos_LL = (float)(-1);
		this->m_Trolley_pos_HL = (float)(-1);
	}

	//TVR LIMITS//
	retSt = GetTagLimits(theApp.m_pDlg->m_tiTVRotSp, &llim, &hlim);
	if(retSt == RT_NORMAL){
		_stprintf_s(text, "%.0f", llim);
		this->m_TVR_LL_c.SetWindowText(text);
		this->m_TVR_LL = llim;
		_stprintf_s(text, "%.0f", hlim);
		this->m_TVR_HL_c.SetWindowText(text);
		this->m_TVR_HL = hlim;
	}
	else{
		this->m_TVR_LL_c.SetWindowText("ERR");
		this->m_TVR_HL_c.SetWindowText("ERR");
		this->m_TVR_LL = (float)(-1);
		this->m_TVR_HL = (float)(-1);
	}

	//MM LIMITS//
	retSt = GetTagLimits(theApp.m_pDlg->m_tiMMSp, &llim, &hlim);
	if(retSt == RT_NORMAL){
		_stprintf_s(text, "%.0f", llim);
		this->m_MM_pos_LL_c.SetWindowText(text);
		this->m_MM_pos_LL = llim;
		_stprintf_s(text, "%.0f", hlim);
		this->m_MM_pos_HL_c.SetWindowText(text);
		this->m_MM_pos_HL = hlim;
	}
	else{
		this->m_MM_pos_LL_c.SetWindowText("ERR");
		this->m_MM_pos_HL_c.SetWindowText("ERR");
		this->m_MM_pos_LL = (float)(-1);
		this->m_MM_pos_HL = (float)(-1);
	}

	//TVM LIMITS//
	retSt = GetTagLimits(theApp.m_pDlg->m_tiTVSp, &llim, &hlim);
	if(retSt == RT_NORMAL){
		_stprintf_s(text, "%.0f", llim);
		this->m_TV_pos_LL_c.SetWindowText(text);
		this->m_TV_pos_LL = llim;
		_stprintf_s(text, "%.0f", hlim);
		this->m_TV_pos_HL_c.SetWindowText(text);
		this->m_TV_pos_HL = hlim;
	}
	else{
		this->m_TV_pos_LL_c.SetWindowText("ERR");
		this->m_TV_pos_HL_c.SetWindowText("ERR");
		this->m_TV_pos_LL = (float)(-1);
		this->m_TV_pos_HL = (float)(-1);
	}

	//MMR LIMITS//
	retSt = GetTagLimits(theApp.m_pDlg->m_tiMMRotSp, &llim, &hlim);
	if(retSt == RT_NORMAL){
		_stprintf_s(text, "%.0f", llim);
		this->m_MMR_LL_c.SetWindowText(text);
		_stprintf_s(text, "%.0f", hlim);
		this->m_MMR_HL_c.SetWindowText(text);
		this->m_MMR_LL = llim;
		this->m_MMR_HL = hlim;
	}
	else{
		this->m_MMR_LL_c.SetWindowText("ERR");
		this->m_MMR_HL_c.SetWindowText("ERR");
		this->m_MMR_LL = (float)(-1);
		this->m_MMR_HL = (float)(-1);
	}
		
	//MM Clus LIMITS//
	retSt = GetTagLimits(theApp.m_pDlg->m_tiMMClusSp, &llim, &hlim);
	if(retSt == RT_NORMAL){
		_stprintf_s(text, "%.0f", llim);
		this->m_MMClus_HL_c.SetWindowText(text);
		_stprintf_s(text, "%.0f", hlim);
		this->m_MMR_HL_c.SetWindowText(text);
		this->m_MMClus_LL = llim;
		this->m_MMClus_HL = hlim;
	}
	else{
		this->m_MMClus_LL_c.SetWindowText("ERR");
		this->m_MMClus_HL_c.SetWindowText("ERR");
		this->m_MMClus_LL = (float)(-1);
		this->m_MMClus_HL = (float)(-1);
	}
*/
	//OnSpChanged(0,0);

	if(!this->m_MovInProg)
		SetDynSPRefresh(1);
	
	return(true);
}

void ManualMode::OnClose(){
	if(this->m_MovInProg){
		//MessageBox("Cannot exit, a movement is in progress.", "Warning", MB_OK | MB_ICONERROR);
		_stprintf_s(this->m_msg, _T("Cannot exit, a movement is in progress."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	theApp.m_pDlg->m_ManModeOpened = false;
	CDialog::OnClose();
}

void ManualMode::OnDestroy(){
	if(this->m_MovInProg){
		//MessageBox("Cannot exit, a movement is in progress.", "Warning", MB_OK | MB_ICONERROR);
		_stprintf_s(this->m_msg, _T("Cannot exit, a movement is in progress."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	CDialog::OnDestroy();
	theApp.m_pDlg->m_ManModeOpened = false;	
}


void ManualMode::OnDummy(){
	RETSTAT		retSt;
	RETCOND		retCnd;
	TAGVALEX	tagVal;
	HCURSOR		prev;
	int			retInt;
	int			spCount;
	int			retInt_3;
	float		sp;
	float		lowLim;
	float		highLim;
	bool		spOk;
	CString		str;

	if(this->m_MovInProg){
		_stprintf_s(this->m_msg, _T("Cannot change set point while a movement is in progress."));
		this->InsertMessage(MSG_WRN, this->m_msg);
		return;
	}

	//Manage Enter from SP edit boxes//
	this->UpdateData(true);
 	switch(this->m_MovSelected){
	case MOV_NULL:
		break;
	case MOV_BRIDGE:
		this->m_Bridge_sp_c.GetWindowText(str);
		if(!_tcscmp(str, _T(""))){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Bridge movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = _stscanf_s(str, _T("%f"), &sp);
		if(retInt == 0){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Bridge movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_Bridge_pos_LL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Bridge Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &lowLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Bridge Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_Bridge_pos_HL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Bridge Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &highLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Bridge Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = this->CheckSp(sp, this->m_Bridge_pos, lowLim, highLim);
		if(retInt == SP_OK){
			retSt = VotedTagWrite(TWN_BRIDGE_SP, 0, sp, FLT_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error writing Bridge setpoint value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			//Rilettura SP//
			prev = SetCursor(this->m_curHour);
			Sleep(SP_CHECK_INIT_WAIT);
			spOk = false;
			spCount = SP_CHECK_CYCLES;
			while(spCount > 0){
				Sleep(SP_CHECK_WAIT);

				retCnd = TagRead(&theApp.m_pDlg->m_tiBridgeSp, &tagVal);
				if(retCnd != R_NORMAL)
					spOk = false;
				else{
					if((tagVal.value.val_float < (float)(sp - TOLL_MM)) || (tagVal.value.val_float > (float)(sp + TOLL_MM)))
						spOk = false;
					else
						spOk = true;
				}

				spCount--;

				if(spOk)
					break;
			}

			prev = SetCursor(this->m_curNormal);

			if(!spOk){
				_stprintf_s(this->m_msg, _T("Error setting bridge set point value; try again please."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			_stprintf_s(this->m_msg, _T("Bridge set point successfully updated."));
			this->InsertMessage(MSG_OK, this->m_msg);
		}
		else if(retInt == SP_OUT_OF_BOUND){
			_stprintf_s(this->m_msg, _T("Bridge set point inserted is out of bounds."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		else{
			_stprintf_s(this->m_msg, _T("Bridge is already is the selected position."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		break;
	case MOV_TROLLEY:
		this->m_Trolley_sp_c.GetWindowText(str);
		if(!_tcscmp(str, _T(""))){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Trolley movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = _stscanf_s(str, _T( "%f"), &sp);
		if(retInt == 0){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Trolley movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_Trolley_pos_LL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Trolley Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &lowLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Trolley Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_Trolley_pos_HL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Trolley Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &highLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Trolley Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retInt = this->CheckSp(sp, this->m_Trolley_pos, lowLim, highLim);
		if(retInt == SP_OK){
			retSt = VotedTagWrite(TWN_TROLLEY_SP, 0, sp, FLT_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error writing Trolley setpoint value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			//Rilettura SP//
			prev = SetCursor(this->m_curHour);
			Sleep(SP_CHECK_INIT_WAIT);
			spOk = false;
			spCount = SP_CHECK_CYCLES;
			while(spCount > 0){
				Sleep(SP_CHECK_WAIT);

				retCnd = TagRead(&theApp.m_pDlg->m_tiTrolleySp, &tagVal);
				if(retCnd != R_NORMAL)
					spOk = false;
				else{
					if((tagVal.value.val_float < (float)(sp - TOLL_MM)) || (tagVal.value.val_float > (float)(sp + TOLL_MM)))
						spOk = false;
					else
						spOk = true;
				}

				spCount--;

				if(spOk)
					break;
			}

			prev = SetCursor(this->m_curNormal);

			if(!spOk){
				_stprintf_s(this->m_msg, _T("Error setting trolley set point value; try again please."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			_stprintf_s(this->m_msg, _T("Trolley set point successfully updated."));
			this->InsertMessage(MSG_OK, this->m_msg);
		}
		else if(retInt == SP_OUT_OF_BOUND){
			_stprintf_s(this->m_msg, _T("Trolley set point inserted is out of bounds."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		else{
			_stprintf_s(this->m_msg, _T("Trolley is already is the selected position."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		break;
	case MOV_MM:
		this->m_MM_sp_c.GetWindowText(str);
		if(!_tcscmp(str, _T(""))){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Main mast movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = _stscanf_s(str, _T("%f"), &sp);
		if(retInt == 0){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Main mast movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_MM_pos_LL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &lowLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_MM_pos_HL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &highLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = this->CheckSp((float)(sp), (float)(this->m_MM_pos), lowLim, (float)(highLim + (double)(MM_SP_LOWER_TOLL)));
		if(retInt == SP_OK){
			retSt = VotedTagWrite(TWN_FAGR_SP, 0, sp, FLT_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error writing Main mast setpoint value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			//Rilettura SP//
			prev = SetCursor(this->m_curHour);
			Sleep(SP_CHECK_INIT_WAIT);
			spOk = false;
			spCount = SP_CHECK_CYCLES;
			while(spCount > 0){
				Sleep(SP_CHECK_WAIT);

				retCnd = TagRead(&theApp.m_pDlg->m_tiMMSp, &tagVal);
				if(retCnd != R_NORMAL)
					spOk = false;
				else{
					if((tagVal.value.val_float < (float)(sp - TOLL_MM)) || (tagVal.value.val_float > (float)(sp + TOLL_MM)))
						spOk = false;
					else
						spOk = true;
				}

				spCount--;

				if(spOk)
					break;
			}

			prev = SetCursor(this->m_curNormal);

			if(!spOk){
				_stprintf_s(this->m_msg, _T("Error setting Main Mast set point value; try again please."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			_stprintf_s(this->m_msg, _T("Main mast set point successfully updated."));
			this->InsertMessage(MSG_OK, this->m_msg);
		}
		else if(retInt == SP_OUT_OF_BOUND){
			_stprintf_s(this->m_msg, _T("Main mast set point inserted is out of bounds."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		else{
			_stprintf_s(this->m_msg, _T("Main mast is already is the selected position."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		break;
	case MOV_MM_CLUS:
		this->m_Clus_sp_c.GetWindowText(str);
		if(!_tcscmp(str, _T(""))){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Main mast cluster movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = _stscanf_s(str, _T("%f"), &sp);
		if(retInt == 0){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Main mast cluster movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_MMClus_LL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast cluster Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &lowLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast cluster Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_MMClus_HL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast cluster Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T( "%f"), &highLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast cluster Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = this->CheckSp(sp, this->m_Clus_pos, lowLim, highLim);
		if(retInt == SP_OK){
			retSt = VotedTagWrite(TWN_CLGR_SP, 0, sp, FLT_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error writing Main mast cluster setpoint value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			//Rilettura SP//
			prev = SetCursor(this->m_curHour);
			Sleep(SP_CHECK_INIT_WAIT);
			spOk = false;
			spCount = SP_CHECK_CYCLES;
			while(spCount > 0){
				Sleep(SP_CHECK_WAIT);

				retCnd = TagRead(&theApp.m_pDlg->m_tiMMClusSp, &tagVal);
				if(retCnd != R_NORMAL)
					spOk = false;
				else{
					if((tagVal.value.val_float < (float)(sp - TOLL_MM)) || (tagVal.value.val_float > (float)(sp + TOLL_MM)))
						spOk = false;
					else
						spOk = true;
				}

				spCount--;

				if(spOk)
					break;
			}

			prev = SetCursor(this->m_curNormal);

			if(!spOk){
				_stprintf_s(this->m_msg, _T("Error setting main mast cluster set point value; try again please."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			_stprintf_s(this->m_msg, _T("Main mast cluster gripper set point successfully updated."));
			this->InsertMessage(MSG_OK, this->m_msg);
		}
		else if(retInt == SP_OUT_OF_BOUND){
			_stprintf_s(this->m_msg, _T("Main mast cluster set point inserted is out of bounds."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		else{
			_stprintf_s(this->m_msg, _T("Main mast cluster is already is the selected position."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		break;
	case MOV_MM_ROT:
		this->m_MMR_sp_c.GetWindowText(str);
		if(!_tcscmp(str, _T(""))){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Main mast rotation movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = _stscanf_s(str, _T("%f"), &sp);
		if(retInt == 0){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for Main mast rotation movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_MMR_LL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast rotation Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &lowLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast rotation Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_MMR_HL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast rotation Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &highLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for Main mast rotation Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = this->CheckSp(sp, this->m_MMR_pos, lowLim, highLim);
		if((retInt == SP_OK) || (retInt == SP_IN_POS)){
			retSt = VotedTagWrite(TWN_MM_ROT_SP, 0, sp, FLT_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error writing Main mast rotation setpoint value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			//Rilettura SP//
			prev = SetCursor(this->m_curHour);
			Sleep(SP_CHECK_INIT_WAIT);
			spOk = false;
			spCount = SP_CHECK_CYCLES;
			while(spCount > 0){
				Sleep(SP_CHECK_WAIT);

				retCnd = TagRead(&theApp.m_pDlg->m_tiMMRotSp, &tagVal);
				if(retCnd != R_NORMAL)
					spOk = false;
				else{
					if((tagVal.value.val_float < (float)(sp - TOLL_MM)) || (tagVal.value.val_float > (float)(sp + TOLL_MM)))
						spOk = false;
					else
						spOk = true;
				}

				spCount--;

				if(spOk)
					break;
			}

			prev = SetCursor(this->m_curNormal);

			if(!spOk){
				_stprintf_s(this->m_msg, _T("Error setting main mast rotation set point value; try again please."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			_stprintf_s(this->m_msg, _T("Main mast rotation set point successfully updated."));
			this->InsertMessage(MSG_OK, this->m_msg);
		}
		else if(retInt == SP_OUT_OF_BOUND){
			_stprintf_s(this->m_msg, _T("Main mast rotation set point inserted is out of bounds."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		/*
		else{
			_stprintf_s(this->m_msg, "Main mast rotation is already is the selected position.");
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		*/
		break;
	case MOV_TV:
		this->m_TV_sp_c.GetWindowText(str);
		if(!_tcscmp(str, _T(""))){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for TV mast movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = _stscanf_s(str, _T("%f"), &sp);
		if(retInt == 0){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for TV mast movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_TV_pos_LL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &lowLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_TV_pos_HL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &highLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = this->CheckSp(sp, this->m_TV_pos, lowLim, highLim);
		if(retInt == SP_OK){
			retSt = VotedTagWrite(TWN_TVM_SP, 0, sp, FLT_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error writing TV mast setpoint value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			//Rilettura SP//
			prev = SetCursor(this->m_curHour);
			Sleep(SP_CHECK_INIT_WAIT);
			spOk = false;
			spCount = SP_CHECK_CYCLES;
			while(spCount > 0){
				Sleep(SP_CHECK_WAIT);

				retCnd = TagRead(&theApp.m_pDlg->m_tiTVSp, &tagVal);
				if(retCnd != R_NORMAL)
					spOk = false;
				else{
					if((tagVal.value.val_float < (float)(sp - TOLL_MM)) || (tagVal.value.val_float > (float)(sp + TOLL_MM)))
						spOk = false;
					else
						spOk = true;
				}

				spCount--;

				if(spOk)
					break;
			}

			prev = SetCursor(this->m_curNormal);

			if(!spOk){
				_stprintf_s(this->m_msg, _T("Error setting TV mast set point value; try again please."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			_stprintf_s(this->m_msg, _T("TV mast set point successfully updated."));
			this->InsertMessage(MSG_OK, this->m_msg);
		}
		else if(retInt == SP_OUT_OF_BOUND){
			_stprintf_s(this->m_msg, _T("TV mast set point inserted is out of bounds."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		else{
			_stprintf_s(this->m_msg, _T("TV mast is already is the selected position."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		break;
	case MOV_TV_ROT:
		this->m_TVR_sp_c.GetWindowText(str);
		if(!_tcscmp(str, _T(""))){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for TV mast rotation movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = _stscanf_s(str, _T("%f"), &sp);
		if(retInt == 0){
			_stprintf_s(this->m_msg, _T("Enter a valid set point for TV mast rotation movement."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_TVR_LL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast rotation Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &lowLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast rotation Sp low limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		this->m_TVR_HL_c.GetWindowText(str);
		if(str.IsEmpty()){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast rotation Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt_3 = _stscanf_s(str, _T("%f"), &highLim);
		if(retInt_3 == 0){
			_stprintf_s(this->m_msg, _T("Invalid value for TV mast rotation Sp high limit."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retInt = this->CheckSp(sp, this->m_TVR_pos, lowLim, highLim);
		if(retInt == SP_OK){
			retSt = VotedTagWrite(TWN_TV_ROT_SP, 0, sp, FLT_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Error writing TV mast rotation setpoint value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			//Rilettura SP//
			prev = SetCursor(this->m_curHour);
			Sleep(SP_CHECK_INIT_WAIT);
			spOk = false;
			spCount = SP_CHECK_CYCLES;
			while(spCount > 0){
				Sleep(SP_CHECK_WAIT);

				retCnd = TagRead(&theApp.m_pDlg->m_tiTVRotSp, &tagVal);
				if(retCnd != R_NORMAL)
					spOk = false;
				else{
					if((tagVal.value.val_float < (float)(sp - TOLL_MM)) || (tagVal.value.val_float > (float)(sp + TOLL_MM)))
						spOk = false;
					else
						spOk = true;
				}

				spCount--;

				if(spOk)
					break;
			}

			prev = SetCursor(this->m_curNormal);

			if(!spOk){
				_stprintf_s(this->m_msg, _T("Error setting TV mast rotation set point value; try again please."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}

			_stprintf_s(this->m_msg, _T("TV mast rotation set point successfully updated."));
			this->InsertMessage(MSG_OK, this->m_msg);
		}
		else if(retInt == SP_OUT_OF_BOUND){
			_stprintf_s(this->m_msg, _T("TV mast rotation set point inserted is out of bounds."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		else{
			_stprintf_s(this->m_msg, _T("TV mast rotation is already is the selected position."));
			this->InsertMessage(MSG_WRN, this->m_msg);
			return;
		}
		break;
	default:
		break;
	}
}

void ManualMode::ReadFloatPos(){
	RETCOND		retCnd;
	TAGVALEX	tagVal;
	bool		fixOp;
	bool		displUp;

	//BRIDGE
	retCnd = TagRead(&theApp.m_pDlg->m_tiBridgePos, &tagVal);
	if(retCnd != R_NORMAL)
		return;
	
	this->m_Bridge_pos = tagVal.value.val_float;
	_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
	this->m_Bridge_pos_c.SetWindowText(this->m_valStr);

	//TROLLEY
	retCnd = TagRead(&theApp.m_pDlg->m_tiTrolleyPos, &tagVal);
	if(retCnd != R_NORMAL)
		return;
		
	this->m_Trolley_pos = tagVal.value.val_float;
	_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
	this->m_Trolley_pos_c.SetWindowText(this->m_valStr);

	//MM POS
	retCnd = TagRead(&theApp.m_pDlg->m_tiMMPos, &tagVal);
	if(retCnd != R_NORMAL)
		return;
		
	this->m_MM_pos = tagVal.value.val_float;
	_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
	this->m_MM_pos_c.SetWindowText(this->m_valStr);

	//MM FIX
	retCnd = TagRead(&theApp.m_pDlg->m_tiMMFixOp, &tagVal);
	if(retCnd != R_NORMAL)
		return;

	if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
		fixOp = true;
	else
		fixOp = false;

	retCnd = TagRead(&theApp.m_pDlg->m_tiMMFixCl, &tagVal);
	if(retCnd != R_NORMAL)
		return;
		
	if((GtBit((short int)(tagVal.value.val_di), 15) == 1) && !fixOp)
		this->m_MM_fix_pos.Format(_T("CLOSED"));
	else if((GtBit((short int)(tagVal.value.val_di), 15) == 0) && fixOp)
		this->m_MM_fix_pos.Format(_T("OPEN"));
	else
		this->m_MM_fix_pos.Format(_T("----"));

	//MM CLUS
	retCnd = TagRead(&theApp.m_pDlg->m_tiMMClusPos, &tagVal);
	if(retCnd != R_NORMAL)
		return;
		
	this->m_Clus_pos = tagVal.value.val_float;
	_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
	this->m_Clus_pos_c.SetWindowText(this->m_valStr);

	//MM ROT
	retCnd = TagRead(&theApp.m_pDlg->m_tiMMRotPos, &tagVal);
	if(retCnd != R_NORMAL)
		return;
		
	this->m_MMR_pos = tagVal.value.val_float;
	_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
	this->m_MMR_pos_c.SetWindowText(this->m_valStr);

	//TV POS
	retCnd = TagRead(&theApp.m_pDlg->m_tiTVPos, &tagVal);
	if(retCnd != R_NORMAL)
		return;
		
	this->m_TV_pos = tagVal.value.val_float;
	_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
	this->m_TV_pos_c.SetWindowText(this->m_valStr);

	//TV ROT
	retCnd = TagRead(&theApp.m_pDlg->m_tiTVRotPos, &tagVal);
	if(retCnd != R_NORMAL)
		return;
		
	this->m_TVR_pos = tagVal.value.val_float;
	_stprintf_s(this->m_valStr, _T("%.1f"), tagVal.value.val_float);
	this->m_TVR_pos_c.SetWindowText(this->m_valStr);

	//MM DISPL//
	retCnd = TagRead(&theApp.m_pDlg->m_tiMMDispl_hi_req, &tagVal);
	if(retCnd != R_NORMAL)
		return;

	if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
		displUp = true;
	else
		displUp = false;

	retCnd = TagRead(&theApp.m_pDlg->m_tiMMDispl_lo_req, &tagVal);
	if(retCnd != R_NORMAL)
		return;
		
	if((GtBit((short int)(tagVal.value.val_di), 15) == 1) && !displUp)
		this->m_MM_fix_pos.Format(_T("DOWNWARD"));
	else if((GtBit((short int)(tagVal.value.val_di), 15) == 0) && displUp)
		this->m_MM_fix_pos.Format(_T("UPWARD"));
	else
		this->m_MM_fix_pos.Format(_T("----"));
}

void ManualMode::OnSelchangeCmbFixSp(){
	RETCOND			retCnd;
	RETSTAT			retSt;
	TAGVALEX		tagVal;

	this->m_cmb_MMFix_sp.UpdateData(TRUE);
			
	retCnd = TagRead(&theApp.m_pDlg->m_tiMMFixMreq, &tagVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(this->m_msg, _T("Cannot check Main Mast Latch movement request value."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	if(this->m_cmb_MMFix_sp.GetCurSel() == CMB_MM_FIX_OP){
		if(IsPerm(theApp.m_pDlg->m_tiMMUnfixReq)){
			retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast Latch open request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 0, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast Latch close request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
		}
		else{
			_stprintf_s(this->m_msg, _T("Main Mast Latch unfix request tag is not permissive for set."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
	}
	else if(this->m_cmb_MMFix_sp.GetCurSel() == CMB_MM_FIX_CL){
		if(IsPerm(theApp.m_pDlg->m_tiMMFixReq)){
			retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast Latch close request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 0, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast Latch open request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
		}
		else{
			_stprintf_s(this->m_msg, _T("Main Mast Latch fix request tag is not permissive for set."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
	}	
	_stprintf_s(this->m_msg, _T("Main mast gripper latch set point successfully updated."));
	this->InsertMessage(MSG_OK, this->m_msg);

	this->LoadSP();
}

void ManualMode::OnSelchangeCmbDispl(){
	RETSTAT			retSt;

	this->m_cmbMMDispl.UpdateData(TRUE);

	if((!_tcscmp(this->m_MMDisp_pos, _T("UP"))) && (this->m_cmbMMDispl.GetCurSel() == CMB_MM_DISPL_UP)){
		_stprintf_s(this->m_msg, _T("Main mast displacing is already in upward position."));
		this->InsertMessage(MSG_WRN, this->m_msg);
		return;
	}
	if((!_tcscmp(this->m_MMDisp_pos, _T("DOWN"))) && (this->m_cmbMMDispl.GetCurSel() == CMB_MM_DISPL_DOWN)){
		_stprintf_s(this->m_msg, _T("Main mast displacing is already in downward position."));
		this->InsertMessage(MSG_WRN, this->m_msg);
		return;
	}

	if(this->m_cmbMMDispl.GetCurSel() == CMB_MM_DISPL_UP){
		if(IsPerm(theApp.m_pDlg->m_tiMMDispl_hi_req)){
			retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast Displacing upward request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 0, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot reset Main Mast Displacing downward request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
		}
		else{
			_stprintf_s(this->m_msg, _T("Main Mast Displacing upward request tag is not permissive for set."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
	}
	else if(this->m_cmbMMDispl.GetCurSel() == CMB_MM_DISPL_DOWN){
		if(IsPerm(theApp.m_pDlg->m_tiMMDispl_lo_req)){
			retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write Main Mast Displacing downward request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 0, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot reset Main Mast Displacing upward request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
		}
		else{
			_stprintf_s(this->m_msg, _T("Main Mast Displacing downward request tag is not permissive for set."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
	}
	_stprintf_s(this->m_msg, _T("Main mast Displacing set point successfully updated."));
	this->InsertMessage(MSG_OK, this->m_msg);
}

void ManualMode::OnBtnRefresh(){
	HCURSOR			prev;
	
	if(this->m_MovInProg)
		return;

	prev = SetCursor(this->m_curHour);

	SetDynSPRefresh(1);
	
	Sleep(3000);

	this->LoadLimits();

	prev = SetCursor(this->m_curNormal);
}

void ManualMode::SetBmp(void){
	RETCOND		retCnd;
	TAGVALEX	tagVal;
	BOOL		perm;
	int			i;

	for(i = MOV_BRIDGE; i <= MOV_MM_DISPL; i++){
		switch(i){
		case MOV_BRIDGE:
			retCnd = TagRead(&theApp.m_pDlg->m_tiBridgeMreq, &tagVal);
			if(retCnd != R_NORMAL)
				continue;

			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				this->m_rectBridge.SetBitmap(this->m_bmpGreen);
			else{
				perm = IsPerm(theApp.m_pDlg->m_tiBridgeMreq);
				if(!perm)
					this->m_rectBridge.SetBitmap(this->m_bmpRed);
				else
					this->m_rectBridge.SetBitmap(this->m_bmpGray);
			}
			break;
		case MOV_TROLLEY:
			retCnd = TagRead(&theApp.m_pDlg->m_tiTrolleyMreq, &tagVal);
			if(retCnd != R_NORMAL)
				continue;

			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				this->m_rectTrolley.SetBitmap(this->m_bmpGreen);
			else{
				perm = IsPerm(theApp.m_pDlg->m_tiTrolleyMreq);
				if(!perm)
					this->m_rectTrolley.SetBitmap(this->m_bmpRed);
				else
					this->m_rectTrolley.SetBitmap(this->m_bmpGray);
			}
			break;
		case MOV_MM:
			retCnd = TagRead(&theApp.m_pDlg->m_tiMMMreq, &tagVal);
			if(retCnd != R_NORMAL)
				continue;

			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				this->m_rectFAGR.SetBitmap(this->m_bmpGreen);
			else{
				perm = IsPerm(theApp.m_pDlg->m_tiMMMreq);
				if(!perm)
					this->m_rectFAGR.SetBitmap(this->m_bmpRed);
				else
					this->m_rectFAGR.SetBitmap(this->m_bmpGray);
			}
			break;
		case MOV_MM_FIX:
			if(this->m_cmb_MMFix_sp.GetCurSel() == CMB_MM_FIX_OP){
				retCnd = TagRead(&theApp.m_pDlg->m_tiMMUnfixReq, &tagVal);
				if(retCnd != R_NORMAL)
					continue;

				if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
					this->m_rectFAGRL.SetBitmap(this->m_bmpGreen);
				else{
					perm = IsPerm(theApp.m_pDlg->m_tiMMUnfixReq);
					if(!perm)
						this->m_rectFAGRL.SetBitmap(this->m_bmpRed);
					else
						this->m_rectFAGRL.SetBitmap(this->m_bmpGray);
				}
			}
			else{
				retCnd = TagRead(&theApp.m_pDlg->m_tiMMFixReq, &tagVal);
				if(retCnd != R_NORMAL)
					continue;

				if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
					this->m_rectFAGRL.SetBitmap(this->m_bmpGreen);
				else{
					perm = IsPerm(theApp.m_pDlg->m_tiMMFixReq);
					if(!perm)
						this->m_rectFAGRL.SetBitmap(this->m_bmpRed);
					else
						this->m_rectFAGRL.SetBitmap(this->m_bmpGray);
				}
			}
			break;
		case MOV_MM_CLUS:
			retCnd = TagRead(&theApp.m_pDlg->m_tiMMClusMreq, &tagVal);
			if(retCnd != R_NORMAL)
				continue;

			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				this->m_rectClus.SetBitmap(this->m_bmpGreen);
			else{
				perm = IsPerm(theApp.m_pDlg->m_tiMMClusMreq);
				if(!perm)
					this->m_rectClus.SetBitmap(this->m_bmpRed);
				else
					this->m_rectClus.SetBitmap(this->m_bmpGray);
			}
			break;
		case MOV_MM_ROT:
			retCnd = TagRead(&theApp.m_pDlg->m_tiMMRotMreq, &tagVal);
			if(retCnd != R_NORMAL)
				continue;
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				this->m_rectMMRot.SetBitmap(this->m_bmpGreen);
			else{
				perm = IsPerm(theApp.m_pDlg->m_tiMMRotMreq);
				if(!perm)
					this->m_rectMMRot.SetBitmap(this->m_bmpRed);	
				else
					this->m_rectMMRot.SetBitmap(this->m_bmpGray);
			}
			break;
		case MOV_TV:
			retCnd = TagRead(&theApp.m_pDlg->m_tiTVMreq, &tagVal);
			if(retCnd != R_NORMAL)
				continue;
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				this->m_rectTV.SetBitmap(this->m_bmpGreen);
			else{
				perm = IsPerm(theApp.m_pDlg->m_tiTVMreq);
				if(!perm)
					this->m_rectTV.SetBitmap(this->m_bmpRed);
				else
					this->m_rectTV.SetBitmap(this->m_bmpGray);
			}
			break;
		case MOV_TV_ROT:
			retCnd = TagRead(&theApp.m_pDlg->m_tiTVRotMreq, &tagVal);
			if(retCnd != R_NORMAL)
				continue;
			if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
				this->m_rectTvRot.SetBitmap(this->m_bmpGreen);
			else{
				perm = IsPerm(theApp.m_pDlg->m_tiTVRotMreq);
				if(!perm)
					this->m_rectTvRot.SetBitmap(this->m_bmpRed);
				else
					this->m_rectTvRot.SetBitmap(this->m_bmpGray);
			}
			break;
		case MOV_MM_DISPL:
			if(this->m_cmbMMDispl.GetCurSel() == CMB_MM_DISPL_DOWN){
				retCnd = TagRead(&theApp.m_pDlg->m_tiMMDispl_lo_req, &tagVal);
				if(retCnd != R_NORMAL)
					continue;
				if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
					this->m_rectDispl.SetBitmap(this->m_bmpGreen);
				else{
					perm = IsPerm(theApp.m_pDlg->m_tiMMDispl_lo_req);
					if(!perm)
						this->m_rectDispl.SetBitmap(this->m_bmpRed);
					else
						this->m_rectDispl.SetBitmap(this->m_bmpGray);
				}
			}
			else if(this->m_cmbMMDispl.GetCurSel() == CMB_MM_DISPL_UP){
				retCnd = TagRead(&theApp.m_pDlg->m_tiMMDispl_hi_req, &tagVal);
				if(retCnd != R_NORMAL)
					continue;
				if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
					this->m_rectDispl.SetBitmap(this->m_bmpGreen);
				else{
					perm = IsPerm(theApp.m_pDlg->m_tiMMDispl_hi_req);
					if(!perm)
						this->m_rectDispl.SetBitmap(this->m_bmpRed);
					else
						this->m_rectDispl.SetBitmap(this->m_bmpGray);
				}
			}
			break;
		case MOV_FALV:
			if(this->m_cmbFALVL_sp.GetCurSel() == CMB_MM_FALVL_DOWN){
				retCnd = TagRead(&theApp.m_pDlg->m_tiFALVL_dwn_req, &tagVal);
				if(retCnd != R_NORMAL)
					continue;
				if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
					this->m_rectFalv.SetBitmap(this->m_bmpGreen);
				else{
					perm = IsPerm(theApp.m_pDlg->m_tiFALVL_dwn_req);
					if(!perm)
						this->m_rectFalv.SetBitmap(this->m_bmpRed);
					else
						this->m_rectFalv.SetBitmap(this->m_bmpGray);
				}
			}
			else if(this->m_cmbFALVL_sp.GetCurSel() == CMB_MM_FALVL_UP){
				retCnd = TagRead(&theApp.m_pDlg->m_tiFALVL_up_req, &tagVal);
				if(retCnd != R_NORMAL)
					continue;
				if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
					this->m_rectFalv.SetBitmap(this->m_bmpGreen);
				else{
					perm = IsPerm(theApp.m_pDlg->m_tiFALVL_up_req);
					if(!perm)
						this->m_rectFalv.SetBitmap(this->m_bmpRed);
					else
						this->m_rectFalv.SetBitmap(this->m_bmpGray);
				}
			}
			break;
		default:
			break;
		}
	}
	
	retCnd = TagRead(&theApp.m_pDlg->m_tiMMmount, &tagVal);
	if(retCnd != R_NORMAL)
		goto FALVL_MTN;
	if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
		this->m_MMMtnRect.SetBitmap(this->m_bmpGreen);
		this->m_refMast = MAIN_MAST;
	}
	else{
		perm = IsPerm(theApp.m_pDlg->m_tiMMmount);
		if(!perm)
			this->m_MMMtnRect.SetBitmap(this->m_bmpRed);
		else
			this->m_MMMtnRect.SetBitmap(this->m_bmpGray);
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiTVmount, &tagVal);
	if(retCnd != R_NORMAL)
		goto FALVL_MTN;
	if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
		this->m_TVMtnRect.SetBitmap(this->m_bmpGreen);
	else{
		perm = IsPerm(theApp.m_pDlg->m_tiTVmount);
		if(!perm)
			this->m_TVMtnRect.SetBitmap(this->m_bmpRed);
		else
			this->m_TVMtnRect.SetBitmap(this->m_bmpGray);
	}

FALVL_MTN:
	retCnd = TagRead(&theApp.m_pDlg->m_tiFALVMount, &tagVal);
	if(retCnd != R_NORMAL){
		this->m_falvlMounted = false;
		goto DIMP_MTN;
	}
	if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
		this->m_FALVMtnRect.SetBitmap(this->m_bmpGreen);
		this->m_falvlMounted = true;
		this->m_refMast = FALC_MAST;
	}
	else{
		perm = IsPerm(theApp.m_pDlg->m_tiFALVMount);
		if(!perm)
			this->m_FALVMtnRect.SetBitmap(this->m_bmpRed);
		else
			this->m_FALVMtnRect.SetBitmap(this->m_bmpGray);
		this->m_falvlMounted = false;
	}

DIMP_MTN:
	retCnd = TagRead(&theApp.m_pDlg->m_tiDIMPMount, &tagVal);
	if(retCnd != R_NORMAL){
		this->m_dimpMounted = false;
		return;
	}
	if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
		this->m_DIMPMtnRect.SetBitmap(this->m_bmpGreen);
		this->m_dimpMounted = true;
		this->m_refMast = DMP_MAST;
	}
	else{
		perm = IsPerm(theApp.m_pDlg->m_tiDIMPMount);
		if(!perm)
			this->m_DIMPMtnRect.SetBitmap(this->m_bmpRed);
		else
			this->m_DIMPMtnRect.SetBitmap(this->m_bmpGray);
		this->m_dimpMounted = false;
	}
}


void ManualMode::OnBridgeSel(){
	RETSTAT			retSt;
	RETCOND			retCnd;
	BOOL			isPerm;
	TAGVALEX		tagVal;
	HCURSOR			prev;
	int				count;

	if(CheckUser())
		return;

	if(this->m_MovInProg)
		return;

	if(this->m_MovSelected == MOV_BRIDGE){
		retSt = VotedTagWrite(TWN_BRIDGE_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Bridge movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_MovSelected = MOV_NULL;
		this->m_Bridge_sp_c.EnableWindow(FALSE);
		_stprintf_s(this->m_msg, _T("Bridge movement unselected."));
		this->InsertMessage(MSG_OK, this->m_msg);
		return;
	}
	else if(this->m_MovSelected != MOV_NULL)
		this->UnselectMov();

	retCnd = TagRead(&theApp.m_pDlg->m_tiTrolleyMreq, &tagVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(this->m_msg, _T("Cannot read TROLLEY_MREQ tag value."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
		retSt = VotedTagWrite(TWN_TROLLEY_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Trolley movement request reset cannot be completed."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
	}
	
	retCnd = TagRead(&theApp.m_pDlg->m_tiTVRotMreq, &tagVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(this->m_msg, _T("Cannot read TVROT_MREQ tag value."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
		retSt = VotedTagWrite(TWN_TV_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast rotation movement request reset cannot be completed."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
	}

	count = 0;
	prev = SetCursor(this->m_curHour);
	while(!IsPerm(theApp.m_pDlg->m_tiBridgeMreq)){
		if(count == NP_MAX_COUNT)
			break;
		retSt = VotedTagWrite(TWN_TROLLEY_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Trolley movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement FIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement UNFIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_CLGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Cluster gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_MM_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM rotation  movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_TVM_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TV_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast rotation movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALV_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		Sleep(NP_WAIT);
		count++;
	}

	prev = SetCursor(this->m_curNormal);

	isPerm = IsPerm(theApp.m_pDlg->m_tiBridgeMreq);
	if(!isPerm){
		_stprintf_s(this->m_msg, _T("Bridge movement request RCM is currently not permissive for set."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	retSt = VotedTagWrite(TWN_BRIDGE_MREQ, 1, 0, DIG_VALUE);
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("Bridge movement request cannot be completed. Maybe another movement is in progerss or selected."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));
		this->m_cmbFALVL_sp.EnableWindow(FALSE);

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	this->m_btnBridge.SetWindowText(_T("Unselect"));
	this->m_btnTrolley.SetWindowText(_T("Select"));
	this->m_btnMMRot.SetWindowText(_T("Select"));
	this->m_btnMM.SetWindowText(_T("Select"));
	this->m_btnMMFix.SetWindowText(_T("Select"));
	this->m_btnMMClus.SetWindowText(_T("Select"));
	this->m_btnTV.SetWindowText(_T("Select"));
	this->m_btnTVRot.SetWindowText(_T("Select"));
	this->m_btnMMDispl.SetWindowText(_T("Select"));
	this->m_btnFALVL.SetWindowText(_T("Select"));

	this->m_Bridge_sp_c.EnableWindow(TRUE);
	this->m_Trolley_sp_c.EnableWindow(FALSE);
	this->m_MMR_sp_c.EnableWindow(FALSE);
	this->m_MM_sp_c.EnableWindow(FALSE);
	this->m_cmb_MMFix_sp.EnableWindow(FALSE);
	this->m_Clus_sp_c.EnableWindow(FALSE);
	this->m_TVR_sp_c.EnableWindow(FALSE);
	this->m_TV_sp_c.EnableWindow(FALSE);
	this->m_cmbMMDispl.EnableWindow(FALSE);
	this->m_cmbFALVL_sp.EnableWindow(FALSE);

	this->m_MovSelected = MOV_BRIDGE;

	_stprintf_s(this->m_msg, _T("Bridge movement selected."));
	this->InsertMessage(MSG_OK, this->m_msg);

	this->UpdateData(FALSE);
}

void ManualMode::OnTrolleySel(){
	RETCOND			retCnd;
	RETSTAT			retSt;
	BOOL			isPerm;
	TAGVALEX		tagVal;
	HCURSOR			prev;
	int				count;

	if(CheckUser())
		return;
	
	if(this->m_MovInProg)
		return;

	if(this->m_MovSelected == MOV_TROLLEY){
		retSt = VotedTagWrite(TWN_TROLLEY_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Trolley movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_MovSelected = MOV_NULL;
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		_stprintf_s(this->m_msg, _T("Trolley movement unselected."));
		this->InsertMessage(MSG_OK, this->m_msg);
		return;
	}
	else if(this->m_MovSelected != MOV_NULL)
		this->UnselectMov();

	retCnd = TagRead(&theApp.m_pDlg->m_tiBridgeMreq, &tagVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(this->m_msg, _T("Cannot read BRIDGE_MREQ tag value."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
		retSt = VotedTagWrite(TWN_BRIDGE_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Bridge movement request reset cannot be completed."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
	}
	
	retCnd = TagRead(&theApp.m_pDlg->m_tiTVRotMreq, &tagVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(this->m_msg, _T("Cannot read TVROT_MREQ tag value."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
		retSt = VotedTagWrite(TWN_TV_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast rotation movement request reset cannot be completed."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
	}

	count = 0;
	prev = SetCursor(this->m_curHour);
	while(!IsPerm(theApp.m_pDlg->m_tiTrolleyMreq)){
		if(count == NP_MAX_COUNT)
			break;
		retSt = VotedTagWrite(TWN_BRIDGE_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Bridge movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_FAGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement FIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement UNFIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_CLGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Cluster gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_MM_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM rotation  movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_TVM_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TV_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast rotation movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALV_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		Sleep(NP_WAIT);
		count++;
	}

	prev = SetCursor(this->m_curNormal);

	isPerm = IsPerm(theApp.m_pDlg->m_tiTrolleyMreq);
	if(!isPerm){
		_stprintf_s(this->m_msg, _T("Trolley movement request RCM is currently not permissive for set."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	retSt = VotedTagWrite(TWN_TROLLEY_MREQ, 1, 0, DIG_VALUE);
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("Trolley movement request cannot be completed. Maybe another movement is in progerss or selected."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	this->m_btnBridge.SetWindowText(_T("Select"));
	this->m_btnTrolley.SetWindowText(_T("Unselect"));
	this->m_btnMMRot.SetWindowText(_T("Select"));
	this->m_btnMM.SetWindowText(_T("Select"));
	this->m_btnMMFix.SetWindowText(_T("Select"));
	this->m_btnMMClus.SetWindowText(_T("Select"));
	this->m_btnTV.SetWindowText(_T("Select"));
	this->m_btnTVRot.SetWindowText(_T("Select"));
	this->m_btnMMDispl.SetWindowText(_T("Select"));
	this->m_btnFALVL.SetWindowText(_T("Select"));

	this->m_Bridge_sp_c.EnableWindow(FALSE);
	this->m_Trolley_sp_c.EnableWindow(TRUE);
	this->m_MMR_sp_c.EnableWindow(FALSE);
	this->m_MM_sp_c.EnableWindow(FALSE);
	this->m_cmb_MMFix_sp.EnableWindow(FALSE);
	this->m_Clus_sp_c.EnableWindow(FALSE);
	this->m_TVR_sp_c.EnableWindow(FALSE);
	this->m_TV_sp_c.EnableWindow(FALSE);
	this->m_cmbMMDispl.EnableWindow(FALSE);
	this->m_cmbFALVL_sp.EnableWindow(FALSE);
	
	
	this->m_MovSelected = MOV_TROLLEY;

	_stprintf_s(this->m_msg, _T("Trolley movement selected."));
	this->InsertMessage(MSG_OK, this->m_msg);

	this->UpdateData(FALSE);
}

void ManualMode::OnMmRotSel(){
	RETSTAT			retSt;
	BOOL			isPerm;
	HCURSOR			prev;
	int				count;

	if(CheckUser())
		return;

	if(this->m_MovInProg)
		return;

	if(this->m_MovSelected == MOV_MM_ROT){
		retSt = VotedTagWrite(TWN_MM_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Main mast rotation movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_MovSelected = MOV_NULL;
		this->m_MMR_sp_c.EnableWindow(FALSE);
		_stprintf_s(this->m_msg, _T("Main mast rotation movement unselected."));
		this->InsertMessage(MSG_OK, this->m_msg);
		return;
	}
	else if(this->m_MovSelected != MOV_NULL)
		this->UnselectMov();

	count = 0;
	prev = SetCursor(this->m_curHour);
	while(!IsPerm(theApp.m_pDlg->m_tiMMRotMreq)){
		if(count == NP_MAX_COUNT)
			break;
		retSt = VotedTagWrite(TWN_BRIDGE_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Bridge movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TROLLEY_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Trolley movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement FIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement UNFIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_CLGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Cluster gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_TVM_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TV_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast rotation movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALV_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		Sleep(NP_WAIT);
		count++;
	}

	prev = SetCursor(this->m_curNormal);

	isPerm = IsPerm(theApp.m_pDlg->m_tiMMRotMreq);
	if(!isPerm){
		_stprintf_s(this->m_msg, _T("Main mast rotation movement request RCM is currently not permissive for set."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	retSt = VotedTagWrite(TWN_MM_ROT_MREQ, 1, 0, DIG_VALUE);
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("MM rotation  movement request cannot be completed. Maybe another movement is in progerss or selected."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	this->m_btnBridge.SetWindowText(_T("Select"));
	this->m_btnTrolley.SetWindowText(_T("Select"));
	this->m_btnMMRot.SetWindowText(_T("Unselect"));
	this->m_btnMM.SetWindowText(_T("Select"));
	this->m_btnMMFix.SetWindowText(_T("Select"));
	this->m_btnMMClus.SetWindowText(_T("Select"));
	this->m_btnTV.SetWindowText(_T("Select"));
	this->m_btnTVRot.SetWindowText(_T("Select"));
	this->m_btnMMDispl.SetWindowText(_T("Select"));
	this->m_btnFALVL.SetWindowText(_T("Select"));

	this->m_Bridge_sp_c.EnableWindow(FALSE);
	this->m_Trolley_sp_c.EnableWindow(FALSE);
	this->m_MMR_sp_c.EnableWindow(TRUE);
	this->m_MM_sp_c.EnableWindow(FALSE);
	this->m_cmb_MMFix_sp.EnableWindow(FALSE);
	this->m_Clus_sp_c.EnableWindow(FALSE);
	this->m_TVR_sp_c.EnableWindow(FALSE);
	this->m_TV_sp_c.EnableWindow(FALSE);
	this->m_cmbMMDispl.EnableWindow(FALSE);
	this->m_cmbFALVL_sp.EnableWindow(FALSE);

	this->m_MovSelected = MOV_MM_ROT;

	_stprintf_s(this->m_msg, _T("Main mast rotation movement selected."));
	this->InsertMessage(MSG_OK, this->m_msg);

	this->UpdateData(FALSE);
}

void ManualMode::OnMmSel(){
	RETSTAT			retSt;
	BOOL			isPerm;
	HCURSOR			prev;
	int				count;

	if(CheckUser())
		return;
	
	if(this->m_MovInProg)
		return;

	if(this->m_MovSelected == MOV_MM){
		retSt = VotedTagWrite(TWN_FAGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Main mast movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_MovSelected = MOV_NULL;
		this->m_MM_sp_c.EnableWindow(FALSE);
		_stprintf_s(this->m_msg, _T("Main mast movement unselected."));
		this->InsertMessage(MSG_OK, this->m_msg);
		return;
	}
	else if(this->m_MovSelected != MOV_NULL)
		this->UnselectMov();

	count = 0;
	prev = SetCursor(this->m_curHour);
	while(!IsPerm(theApp.m_pDlg->m_tiMMMreq)){
		if(count == NP_MAX_COUNT)
			break;
		retSt = VotedTagWrite(TWN_BRIDGE_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Bridge movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TROLLEY_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Trolley movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement FIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement UNFIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_CLGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Cluster gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_MM_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM rotation  movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_TVM_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TV_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast rotation movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALV_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		Sleep(NP_WAIT);
		count++;
	}

	prev = SetCursor(this->m_curNormal);

	isPerm = IsPerm(theApp.m_pDlg->m_tiMMMreq);
	if(!isPerm){
		_stprintf_s(this->m_msg, _T("Main mast movement request RCM is currently not permissive for set."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	retSt = VotedTagWrite(TWN_FAGR_MREQ, 1, 0, DIG_VALUE);
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("FA gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	this->InsertMessage(MSG_ERR, this->m_msg);
	this->m_btnBridge.SetWindowText(_T("Select"));
	this->m_btnTrolley.SetWindowText(_T("Select"));
	this->m_btnMMRot.SetWindowText(_T("Select"));
	this->m_btnMM.SetWindowText(_T("Unselect"));
	this->m_btnMMFix.SetWindowText(_T("Select"));
	this->m_btnMMClus.SetWindowText(_T("Select"));
	this->m_btnTV.SetWindowText(_T("Select"));
	this->m_btnTVRot.SetWindowText(_T("Select"));
	this->m_btnMMDispl.SetWindowText(_T("Select"));
	this->m_btnFALVL.SetWindowText(_T("Select"));

	this->m_Bridge_sp_c.EnableWindow(FALSE);
	this->m_Trolley_sp_c.EnableWindow(FALSE);
	this->m_MMR_sp_c.EnableWindow(FALSE);
	this->m_MM_sp_c.EnableWindow(TRUE);
	this->m_cmb_MMFix_sp.EnableWindow(FALSE);
	this->m_Clus_sp_c.EnableWindow(FALSE);
	this->m_TVR_sp_c.EnableWindow(FALSE);
	this->m_TV_sp_c.EnableWindow(FALSE);
	this->m_cmbMMDispl.EnableWindow(FALSE);
	this->m_cmbFALVL_sp.EnableWindow(FALSE);

	this->m_MovSelected = MOV_MM;
	
	_stprintf_s(this->m_msg, _T("Main FA gripper movement selected."));
	this->InsertMessage(MSG_OK, this->m_msg);

	this->UpdateData(FALSE);
	
}

void ManualMode::OnMmFixSel(){
	RETSTAT			retSt;
	BOOL			isPerm;
	HCURSOR			prev;
	int				count;

	if(CheckUser())
		return;

	if(this->m_MovInProg)
		return;

	if(this->m_MovSelected == MOV_MM_FIX){
		retSt = VotedTagWrite(TWN_FAGRL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Main mast latch movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Main mast latch movement fix request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Main mast latch movement unfix request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_MovSelected = MOV_NULL;
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		_stprintf_s(this->m_msg, _T("Main mast latch movement unselected."));
		this->InsertMessage(MSG_OK, this->m_msg);
		return;
	}
	else if(this->m_MovSelected != MOV_NULL)
		this->UnselectMov();

	count = 0;
	prev = SetCursor(this->m_curHour);
	while(!IsPerm(theApp.m_pDlg->m_tiMMFixMreq)){
		if(count == NP_MAX_COUNT)
			break;
		retSt = VotedTagWrite(TWN_BRIDGE_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Bridge movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TROLLEY_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Trolley movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_CLGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Cluster gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_MM_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM rotation  movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_TVM_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TV_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast rotation movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALV_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		Sleep(NP_WAIT);
		count++;
	}

	prev = SetCursor(this->m_curNormal);

	isPerm = IsPerm(theApp.m_pDlg->m_tiMMFixMreq);
	if(!isPerm){
		_stprintf_s(this->m_msg, _T("Main mast fixing request RCM is currently not permissive for set."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	retSt = VotedTagWrite(TWN_FAGRL_MREQ, 1, 0, DIG_VALUE);
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("FA gripper latch movement request cannot be completed. Maybe another movement is in progerss or selected."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	this->m_btnBridge.SetWindowText(_T("Select"));
	this->m_btnTrolley.SetWindowText(_T("Select"));
	this->m_btnMMRot.SetWindowText(_T("Select"));
	this->m_btnMM.SetWindowText(_T("Select"));
	this->m_btnMMFix.SetWindowText(_T("Unselect"));
	this->m_btnMMClus.SetWindowText(_T("Select"));
	this->m_btnTV.SetWindowText(_T("Select"));
	this->m_btnTVRot.SetWindowText(_T("Select"));
	this->m_btnMMDispl.SetWindowText(_T("Select"));
	this->m_btnFALVL.SetWindowText(_T("Select"));

	this->m_Bridge_sp_c.EnableWindow(FALSE);
	this->m_Trolley_sp_c.EnableWindow(FALSE);
	this->m_MMR_sp_c.EnableWindow(FALSE);
	this->m_MM_sp_c.EnableWindow(FALSE);
	this->m_cmb_MMFix_sp.EnableWindow(TRUE);
	this->m_Clus_sp_c.EnableWindow(FALSE);
	this->m_TVR_sp_c.EnableWindow(FALSE);
	this->m_TV_sp_c.EnableWindow(FALSE);
	this->m_cmbMMDispl.EnableWindow(FALSE);
	this->m_cmbFALVL_sp.EnableWindow(FALSE);

	this->m_MovSelected = MOV_MM_FIX;

	_stprintf_s(this->m_msg, _T("Main FA gripper latch movement selected."));
	this->InsertMessage(MSG_OK, this->m_msg);

	this->UpdateData(FALSE);
	
}

void ManualMode::OnMmClusSel(){
	RETSTAT			retSt;
	BOOL			isPerm;
	HCURSOR			prev;
	int				count;

	if(CheckUser())
		return;

	if(this->m_MovInProg)
		return;

	if(this->m_MovSelected == MOV_MM_CLUS){
		retSt = VotedTagWrite(TWN_CLGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("Main mast cluster gripper movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Main mast cluster gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_MovSelected = MOV_NULL;
		this->m_Clus_sp_c.EnableWindow(FALSE);
		_stprintf_s(this->m_msg, _T("Main Cluster gripper movement selected."));
		this->InsertMessage(MSG_OK, this->m_msg);
		return;
	}

	count = 0;
	prev = SetCursor(this->m_curHour);
	while(!IsPerm(theApp.m_pDlg->m_tiMMClusMreq)){
		if(count == NP_MAX_COUNT)
			break;
		retSt = VotedTagWrite(TWN_BRIDGE_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Bridge movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TROLLEY_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Trolley movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement FIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement UNFIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_MM_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM rotation  movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_TVM_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TV_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast rotation movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALV_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		Sleep(NP_WAIT);
		count++;
	}

	prev = SetCursor(this->m_curNormal);

	isPerm = IsPerm(theApp.m_pDlg->m_tiMMClusMreq);
	if(!isPerm){
		_stprintf_s(this->m_msg, _T("Main mast cluster gripper request RCM is currently not permissive for set."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	retSt = VotedTagWrite(TWN_CLGR_MREQ, 1, 0, DIG_VALUE);
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("Cluster gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	this->m_btnBridge.SetWindowText(_T("Select"));
	this->m_btnTrolley.SetWindowText(_T("Select"));
	this->m_btnMMRot.SetWindowText(_T("Select"));
	this->m_btnMM.SetWindowText(_T("Select"));
	this->m_btnMMFix.SetWindowText(_T("Select"));
	this->m_btnMMClus.SetWindowText(_T("Unselect"));
	this->m_btnTV.SetWindowText(_T("Select"));
	this->m_btnTVRot.SetWindowText(_T("Select"));
	this->m_btnMMDispl.SetWindowText(_T("Select"));
	this->m_btnFALVL.SetWindowText(_T("Select"));

	this->m_Bridge_sp_c.EnableWindow(FALSE);
	this->m_Trolley_sp_c.EnableWindow(FALSE);
	this->m_MMR_sp_c.EnableWindow(FALSE);
	this->m_MM_sp_c.EnableWindow(FALSE);
	this->m_cmb_MMFix_sp.EnableWindow(FALSE);
	this->m_Clus_sp_c.EnableWindow(TRUE);
	this->m_TVR_sp_c.EnableWindow(FALSE);
	this->m_TV_sp_c.EnableWindow(FALSE);
	this->m_cmbMMDispl.EnableWindow(FALSE);
	this->m_cmbFALVL_sp.EnableWindow(FALSE);

	this->m_MovSelected = MOV_MM_CLUS;
	
	_stprintf_s(this->m_msg, _T("Main Cluster gripper movement selected."));
	this->InsertMessage(MSG_OK, this->m_msg);

	this->UpdateData(FALSE);
	
}

void ManualMode::OnTvRotSel(){
	RETCOND			retCnd;
	RETSTAT			retSt;
	BOOL			isPerm;
	TAGVALEX		tagVal;
	HCURSOR			prev;
	int				count;
	
	if(CheckUser())
		return;

	if(this->m_MovInProg)
		return;

	if(this->m_MovSelected == MOV_TV_ROT){
		retSt = VotedTagWrite(TWN_TV_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("TV mast rotation movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("TV mast rotation movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_OK, this->m_msg);
			return;
		}
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_MovSelected = MOV_NULL;
		this->m_TVR_sp_c.EnableWindow(FALSE);
		_stprintf_s(this->m_msg, _T("TV mast rotation movement selected."));
		this->InsertMessage(MSG_OK, this->m_msg);
		return;
	}


	retCnd = TagRead(&theApp.m_pDlg->m_tiTrolleyMreq, &tagVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(this->m_msg, _T("Cannot read TROLLEY_MREQ tag value."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
		retSt = VotedTagWrite(TWN_TROLLEY_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Trolley movement request reset cannot be completed."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
	}
	
	retCnd = TagRead(&theApp.m_pDlg->m_tiBridgeMreq, &tagVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(this->m_msg, _T("Cannot read BRIDGE_MREQ tag value."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	if(GtBit((short int)(tagVal.value.val_di), 15) == 1){
		retSt = VotedTagWrite(TWN_BRIDGE_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Bridge movement request reset cannot be completed."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
	}

	count = 0;
	prev = SetCursor(this->m_curHour);
	while(!IsPerm(theApp.m_pDlg->m_tiTVRotMreq)){
		if(count == NP_MAX_COUNT)
			break;
		retSt = VotedTagWrite(TWN_BRIDGE_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Bridge movement request reset cannot be completed."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TROLLEY_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Trolley movement request reset cannot be completed."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Main Mast movement request reset cannot be completed."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement FIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement UNFIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Main Mast Latch movement request reset cannot be completed."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_CLGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Main Mast Cluster movement request reset cannot be completed."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_MM_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Main Mast rotation request reset cannot be completed."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_TVM_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV Mast movement request reset cannot be completed."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALV_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		Sleep(NP_WAIT);
		count++;
	}

	prev = SetCursor(this->m_curNormal);

	isPerm = IsPerm(theApp.m_pDlg->m_tiTVRotMreq);
	if(!isPerm){
		_stprintf_s(this->m_msg, _T("TV mast rotation movement request RCM is currently not permissive for set."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	retSt = VotedTagWrite(TWN_TV_ROT_MREQ, 1, 0, DIG_VALUE);
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("TV mast rotation request cannot be completed.\n Maybe another movement is in progerss or selected."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}
	this->m_btnBridge.SetWindowText(_T("Select"));
	this->m_btnTrolley.SetWindowText(_T("Select"));
	this->m_btnMMRot.SetWindowText(_T("Select"));
	this->m_btnMM.SetWindowText(_T("Select"));
	this->m_btnMMFix.SetWindowText(_T("Select"));
	this->m_btnMMClus.SetWindowText(_T("Select"));
	this->m_btnTV.SetWindowText(_T("Select"));
	this->m_btnTVRot.SetWindowText(_T("Unselect"));
	this->m_btnMMDispl.SetWindowText(_T("Select"));
	this->m_btnFALVL.SetWindowText(_T("Select"));

	this->m_Bridge_sp_c.EnableWindow(FALSE);
	this->m_Trolley_sp_c.EnableWindow(FALSE);
	this->m_MMR_sp_c.EnableWindow(FALSE);
	this->m_MM_sp_c.EnableWindow(FALSE);
	this->m_cmb_MMFix_sp.EnableWindow(FALSE);
	this->m_Clus_sp_c.EnableWindow(FALSE);
	this->m_TVR_sp_c.EnableWindow(TRUE);
	this->m_TV_sp_c.EnableWindow(FALSE);
	this->m_cmbMMDispl.EnableWindow(FALSE);
	this->m_cmbFALVL_sp.EnableWindow(FALSE);

	this->m_MovSelected = MOV_TV_ROT;

	_stprintf_s(this->m_msg, _T("TV rotation movement selected."));
	this->InsertMessage(MSG_OK, this->m_msg);

	this->UpdateData(FALSE);	

	
}

void ManualMode::OnTvSel(){
		RETSTAT			retSt;
	BOOL			isPerm;
	HCURSOR			prev;
	int				count;

	if(CheckUser())
		return;

	if(this->m_MovInProg)
		return;

	if(this->m_MovSelected == MOV_TV){
		retSt = VotedTagWrite(TWN_TVM_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast movement request cannot be completed.\n Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_OK, this->m_msg);
			return;
		}
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_MovSelected = MOV_NULL;
		this->m_TV_sp_c.EnableWindow(FALSE);
		_stprintf_s(this->m_msg, _T("TV mast movement selected."));
		this->InsertMessage(MSG_OK, this->m_msg);
		return;
	}

	count = 0;
	prev = SetCursor(this->m_curHour);
	while(!IsPerm(theApp.m_pDlg->m_tiTVMreq)){
		if(count == NP_MAX_COUNT)
			break;
		retSt = VotedTagWrite(TWN_BRIDGE_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Bridge movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TROLLEY_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Trolley movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement FIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement UNFIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_CLGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Cluster gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_MM_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM rotation  movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TV_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast rotation movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALV_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		Sleep(NP_WAIT);
		count++;
	}

	prev = SetCursor(this->m_curNormal);

	isPerm = IsPerm(theApp.m_pDlg->m_tiTVMreq);
	if(!isPerm){
		_stprintf_s(this->m_msg, _T("TV mast movement request RCM is currently not permissive for set."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	retSt = VotedTagWrite(TWN_TVM_MREQ, 1, 0, DIG_VALUE);
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("TV mast movement request cannot be completed. Maybe another movement is in progerss or selected."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		return;
	}

	this->m_btnBridge.SetWindowText(_T("Select"));
	this->m_btnTrolley.SetWindowText(_T("Select"));
	this->m_btnMMRot.SetWindowText(_T("Select"));
	this->m_btnMM.SetWindowText(_T("Select"));
	this->m_btnMMFix.SetWindowText(_T("Select"));
	this->m_btnMMClus.SetWindowText(_T("Select"));
	this->m_btnTV.SetWindowText(_T("Unselect"));
	this->m_btnTVRot.SetWindowText(_T("Select"));
	this->m_btnMMDispl.SetWindowText(_T("Select"));
	this->m_btnFALVL.SetWindowText(_T("Select"));

	this->m_Bridge_sp_c.EnableWindow(FALSE);
	this->m_Trolley_sp_c.EnableWindow(FALSE);
	this->m_MMR_sp_c.EnableWindow(FALSE);
	this->m_MM_sp_c.EnableWindow(FALSE);
	this->m_cmb_MMFix_sp.EnableWindow(FALSE);
	this->m_Clus_sp_c.EnableWindow(FALSE);
	this->m_TVR_sp_c.EnableWindow(FALSE);
	this->m_TV_sp_c.EnableWindow(TRUE);
	this->m_cmbMMDispl.EnableWindow(FALSE);
	this->m_cmbFALVL_sp.EnableWindow(FALSE);

	this->m_MovSelected = MOV_TV;

	_stprintf_s(this->m_msg, _T("TV mast movement selected."));
	this->InsertMessage(MSG_OK, this->m_msg);

	this->UpdateData(FALSE);
}

void ManualMode::OnDisplSel(){
	RETSTAT			retSt;
	BOOL			isPerm;
	HCURSOR			prev;
	TAGIDX			tagIdx;
	int				count;

	if(CheckUser())
		return;
	
	if(this->m_MovInProg)
		return;

	if(this->m_MovSelected == MOV_MM_DISPL){
		retSt = VotedTagWrite(TWN_DISPL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("MM displacing movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("MM displacing movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_OK, this->m_msg);
			return;
		}
		retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("MM displacing downward movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("MM displacing downward movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_OK, this->m_msg);
			return;
		}
		retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("MM displacing upward movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("MM displacing upward movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_OK, this->m_msg);
			return;
		}
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_MovSelected = MOV_NULL;
		this->m_cmbMMDispl.EnableWindow(FALSE);
		_stprintf_s(this->m_msg, _T("MM displacing movement selected."));
		this->InsertMessage(MSG_OK, this->m_msg);
		return;
	}

	if(this->m_cmbMMDispl.GetCurSel() == CMB_MM_DISPL_DOWN)
		memcpy(&tagIdx, &theApp.m_pDlg->m_tiMMDispl_lo_req, sizeof(TAGIDX));
	else if(this->m_cmbMMDispl.GetCurSel() == CMB_MM_DISPL_UP)
		memcpy(&tagIdx, &theApp.m_pDlg->m_tiMMDispl_hi_req, sizeof(TAGIDX));

	count = 0;
	while(!IsPerm(tagIdx)){
		if(count == NP_MAX_COUNT)
			break;

		retSt = VotedTagWrite(TWN_BRIDGE_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("Bridge movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Bridge movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TROLLEY_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("Trolley movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Trolley movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("FA gripper latch movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("FA gripper movement cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("FA gripper latch movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("FA gripper latch movement FIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("FA gripper latch movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("FA gripper latch movement UNFIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("FA gripper latch movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("FA gripper latch movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_CLGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("Cluster gripper movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("Cluster gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_MM_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("MM rotation movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("MM rotation  movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_TVM_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("TV mast movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("TV mast movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TV_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("TV mast rotation request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("TV mast rotation movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_FALV_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FALV UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		Sleep(NP_WAIT);
		count++;
	}

	isPerm = IsPerm(theApp.m_pDlg->m_tiMMDisplMreq);
	if(!isPerm){
		_stprintf_s(this->m_msg, _T("Main mast displacing movement request RCM is currently not permissive for set."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));


		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	retSt = VotedTagWrite(TWN_DISPL_MREQ, 1, 0, DIG_VALUE);
	if(retSt != RT_NORMAL){
		//MessageBox("FA gripper movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
		_stprintf_s(this->m_msg, _T("MM Displacing movement request cannot be completed. Maybe another movement is in progerss or selected."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));


		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	this->m_btnBridge.SetWindowText(_T("Select"));
	this->m_btnTrolley.SetWindowText(_T("Select"));
	this->m_btnMMRot.SetWindowText(_T("Select"));
	this->m_btnMM.SetWindowText(_T("Select"));
	this->m_btnMMFix.SetWindowText(_T("Select"));
	this->m_btnMMClus.SetWindowText(_T("Select"));
	this->m_btnTV.SetWindowText(_T("Select"));
	this->m_btnTVRot.SetWindowText(_T("Select"));
	this->m_btnMMDispl.SetWindowText(_T("Unselect"));
	this->m_btnFALVL.SetWindowText(_T("Select"));


	this->m_Bridge_sp_c.EnableWindow(FALSE);
	this->m_Trolley_sp_c.EnableWindow(FALSE);
	this->m_MMR_sp_c.EnableWindow(FALSE);
	this->m_MM_sp_c.EnableWindow(FALSE);
	this->m_cmb_MMFix_sp.EnableWindow(FALSE);
	this->m_Clus_sp_c.EnableWindow(FALSE);
	this->m_TVR_sp_c.EnableWindow(FALSE);
	this->m_TV_sp_c.EnableWindow(FALSE);
	this->m_cmbMMDispl.EnableWindow(TRUE);
	this->m_cmbFALVL_sp.EnableWindow(FALSE);

	this->m_MovSelected = MOV_MM_DISPL;
	
	_stprintf_s(this->m_msg, _T("MM Displacing movement selected."));
	this->InsertMessage(MSG_OK, this->m_msg);

	this->UpdateData(FALSE);		

	
}

void ManualMode::OnFalvlSel(){
	RETSTAT			retSt;
	BOOL			isPerm;
	HCURSOR			prev;
	TAGIDX			tagIdx;
	int				count;

	if(CheckUser())
		return;

	if(this->m_MovInProg)
		return;

	if(this->m_MovSelected == MOV_FALV){
		retSt = VotedTagWrite(TWN_FALV_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("FALV movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("FALV movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_OK, this->m_msg);
			return;
		}
		retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("FALV UP movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("FALV UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_OK, this->m_msg);
			return;
		}
		retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			//MessageBox("FALV DOWN movement request cannot be completed.\n Maybe another movement is in progerss or selected.", "Warning", MB_OK | MB_ICONINFORMATION);
			_stprintf_s(this->m_msg, _T("FALV DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_OK, this->m_msg);
			return;
		}
		this->m_btnFALVL.SetWindowText(_T("Select"));
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->m_MovSelected = MOV_NULL;
		_stprintf_s(this->m_msg, _T("FALV movement unselected."));
		this->InsertMessage(MSG_OK, this->m_msg);
		return;
	}

	count = 0;

	if(this->m_cmbFALVL_sp.GetCurSel() == CMB_MM_FALVL_DOWN)
		memcpy(&tagIdx, &theApp.m_pDlg->m_tiFALVL_dwn_req, sizeof(TAGIDX));
	else if(this->m_cmbFALVL_sp.GetCurSel() == CMB_MM_FALVL_UP)
		memcpy(&tagIdx, &theApp.m_pDlg->m_tiFALVL_up_req, sizeof(TAGIDX));

	prev = SetCursor(this->m_curHour);
	while(!IsPerm(tagIdx)){
		if(count == NP_MAX_COUNT)
			break;
		retSt = VotedTagWrite(TWN_BRIDGE_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Bridge movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TROLLEY_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Trolley movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_FIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement FIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_UNFIX_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement UNFIX request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_FAGRL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("FA gripper latch movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_CLGR_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Cluster gripper movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_MM_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM rotation  movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}
		
		retSt = VotedTagWrite(TWN_TV_ROT_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("TV mast rotation movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_MREQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing DOWN movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("MM Displacing UP movement request cannot be completed. Maybe another movement is in progerss or selected."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			prev = SetCursor(this->m_curNormal);
			return;
		}

		Sleep(NP_WAIT);
		count++;
	}

	prev = SetCursor(this->m_curNormal);

	isPerm = IsPerm(tagIdx);
	if(!isPerm){
		_stprintf_s(this->m_msg, _T("FALV movement request RCM is currently not permissive for set."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	if(this->m_cmbFALVL_sp.GetCurSel() == CMB_MM_FALVL_DOWN){
		retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 1, 0, DIG_VALUE);
		retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 0, 0, DIG_VALUE);
	}
	else if(this->m_cmbFALVL_sp.GetCurSel() == CMB_MM_FALVL_UP){
		retSt = VotedTagWrite(TWN_DISPL_UP_REQ, 1, 0, DIG_VALUE);
		retSt = VotedTagWrite(TWN_DISPL_DOWN_REQ, 0, 0, DIG_VALUE);
	}
	
	if(retSt != RT_NORMAL){
		_stprintf_s(this->m_msg, _T("FALV movement request cannot be completed. Maybe another movement is in progerss or selected."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		this->m_btnBridge.SetWindowText(_T("Select"));
		this->m_btnTrolley.SetWindowText(_T("Select"));
		this->m_btnMMRot.SetWindowText(_T("Select"));
		this->m_btnMM.SetWindowText(_T("Select"));
		this->m_btnMMFix.SetWindowText(_T("Select"));
		this->m_btnMMClus.SetWindowText(_T("Select"));
		this->m_btnTV.SetWindowText(_T("Select"));
		this->m_btnTVRot.SetWindowText(_T("Select"));
		this->m_btnMMDispl.SetWindowText(_T("Select"));
		this->m_btnFALVL.SetWindowText(_T("Select"));

		this->m_Bridge_sp_c.EnableWindow(FALSE);
		this->m_Trolley_sp_c.EnableWindow(FALSE);
		this->m_MMR_sp_c.EnableWindow(FALSE);
		this->m_MM_sp_c.EnableWindow(FALSE);
		this->m_cmb_MMFix_sp.EnableWindow(FALSE);
		this->m_Clus_sp_c.EnableWindow(FALSE);
		this->m_TVR_sp_c.EnableWindow(FALSE);
		this->m_TV_sp_c.EnableWindow(FALSE);
		this->m_cmbMMDispl.EnableWindow(FALSE);
		this->m_cmbFALVL_sp.EnableWindow(FALSE);
		this->UpdateData(FALSE);
		return;
	}

	this->m_btnBridge.SetWindowText(_T("Select"));
	this->m_btnTrolley.SetWindowText(_T("Select"));
	this->m_btnMMRot.SetWindowText(_T("Select"));
	this->m_btnMM.SetWindowText(_T("Select"));
	this->m_btnMMFix.SetWindowText(_T("Select"));
	this->m_btnMMClus.SetWindowText(_T("Select"));
	this->m_btnTV.SetWindowText(_T("Select"));
	this->m_btnTVRot.SetWindowText(_T("Select"));
	this->m_btnMMDispl.SetWindowText(_T("Select"));
	this->m_btnFALVL.SetWindowText(_T("Unselect"));

	this->m_Bridge_sp_c.EnableWindow(FALSE);
	this->m_Trolley_sp_c.EnableWindow(FALSE);
	this->m_MMR_sp_c.EnableWindow(FALSE);
	this->m_MM_sp_c.EnableWindow(FALSE);
	this->m_cmb_MMFix_sp.EnableWindow(FALSE);
	this->m_Clus_sp_c.EnableWindow(FALSE);
	this->m_TVR_sp_c.EnableWindow(FALSE);
	this->m_TV_sp_c.EnableWindow(FALSE);
	this->m_cmbMMDispl.EnableWindow(FALSE);
	this->m_cmbFALVL_sp.EnableWindow(TRUE);

	this->m_MovSelected = MOV_FALV;

	_stprintf_s(this->m_msg, _T("FALV movement selected."));
	this->InsertMessage(MSG_OK, this->m_msg);

	this->UpdateData(FALSE);
}

void ManualMode::OnMntMm(){
	RETCOND			retCnd;
	RETSTAT			retSt;
	TAGVALEX		tagVal;
	BOOL			perm;

	if(this->m_MovInProg){
		_stprintf_s(this->m_msg, _T("Cannot mount/unmout mast while a movement is in progress."));
		this->InsertMessage(MSG_WRN, this->m_msg);
		return;
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiMMmount, &tagVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(this->m_msg, _T("Error reading MR_MOUNT tag value."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1){
		retSt = VotedTagWrite(TWN_MM_MOUNT, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Error writing MR_MOUNT tag value."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		else{
			_stprintf_s(this->m_msg, _T("Main mast unmounted."));
			this->InsertMessage(MSG_OK, this->m_msg);
			return;
		}
	}
	else{
		perm = IsPerm(theApp.m_pDlg->m_tiMMmount);
		if(!perm){
			_stprintf_s(this->m_msg, _T("MR_MOUNT tag is not permissive for set."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_MM_MOUNT, 1, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Error writing MR_MOUNT tag value."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		else{
			_stprintf_s(this->m_msg, _T("Main mast mounted."));
			this->InsertMessage(MSG_OK, this->m_msg);
			return;
		}
	}
	
}

void ManualMode::OnMntTvm(){
	RETCOND			retCnd;
	RETSTAT			retSt;
	TAGVALEX		tagVal;
	BOOL			perm;

	if(this->m_MovInProg){
		_stprintf_s(this->m_msg, _T("Cannot mount/unmout mast while a movement is in progress."));
		this->InsertMessage(MSG_WRN, this->m_msg);
		return;
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiTVmount, &tagVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(this->m_msg, _T("Error reading TV_MOUNT tag value."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1){
		retSt = VotedTagWrite(TWN_TV_MOUNT, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Error writing TV_MOUNT tag value."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		else{
			_stprintf_s(this->m_msg, _T("TV mast unmounted."));
			this->InsertMessage(MSG_OK, this->m_msg);
			return;
		}
	}
	else{
		perm = IsPerm(theApp.m_pDlg->m_tiTVmount);
		if(!perm){
			_stprintf_s(this->m_msg, _T("TV_MOUNT tag is not permissive for set."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_TV_MOUNT, 1, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Error writing TV_MOUNT tag value."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		else{
			_stprintf_s(this->m_msg, _T("TV mast mounted."));
			this->InsertMessage(MSG_OK, this->m_msg);
			return;
		}
	}
}

void ManualMode::OnMntFalv(){
	RETCOND			retCnd;
	RETSTAT			retSt;
	TAGVALEX		tagVal;
	BOOL			perm;

	if(this->m_MovInProg){
		_stprintf_s(this->m_msg, _T("Cannot mount/unmout mast while a movement is in progress."));
		this->InsertMessage(MSG_WRN, this->m_msg);
		return;
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiFALVMount, &tagVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(this->m_msg, _T("Error reading FALVL_MOUNT tag value."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1){
		retSt = VotedTagWrite(TWN_FALVL_MOUNT, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Error writing FALVL_MOUNT tag value."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		else{
			_stprintf_s(this->m_msg, _T("FALVL mast unmounted."));
			this->InsertMessage(MSG_OK, this->m_msg);
			if(theApp.m_pDlg->m_pPosInfoDialog != NULL)
				theApp.m_pDlg->m_pPosInfoDialog->SetInputCoord(-1, -1);
			return;
		}
	}
	else{
		perm = IsPerm(theApp.m_pDlg->m_tiFALVMount);
		if(!perm){
			_stprintf_s(this->m_msg, _T("FALVL_MOUNT tag is not permissive for set."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}

		retSt = VotedTagWrite(TWN_FALVL_MOUNT, 1, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Error writing FALVL_MOUNT tag value."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		else{
			_stprintf_s(this->m_msg, _T("FALVL mast mounted."));
			this->InsertMessage(MSG_OK, this->m_msg);
			if(theApp.m_pDlg->m_pPosInfoDialog != NULL)
				theApp.m_pDlg->m_pPosInfoDialog->SetInputCoord(-1, -1);
			return;
		}
	}
}

void ManualMode::OnMntDimp(){
	RETCOND			retCnd;
	RETSTAT			retSt;
	TAGVALEX		tagVal;
	BOOL			perm;

	if(this->m_MovInProg){
		_stprintf_s(this->m_msg, _T("Cannot mount/unmout mast while a movement is in progress."));
		this->InsertMessage(MSG_WRN, this->m_msg);
		return;
	}

	retCnd = TagRead(&theApp.m_pDlg->m_tiDIMPMount, &tagVal);
	if(retCnd != R_NORMAL){
		_stprintf_s(this->m_msg, _T("Error reading DIMP_MOUNT tag value."));
		this->InsertMessage(MSG_ERR, this->m_msg);
		return;
	}

	if(GtBit((unsigned short)(tagVal.value.val_di), 15) == 1){
		retSt = VotedTagWrite(TWN_DIMP_MOUNT, 0, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Error writing DIMP_MOUNT tag value."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		else{
			_stprintf_s(this->m_msg, _T("DIMP mast unmounted."));
			this->InsertMessage(MSG_OK, this->m_msg);
			if(theApp.m_pDlg->m_pPosInfoDialog != NULL)
				theApp.m_pDlg->m_pPosInfoDialog->SetInputCoord(-1, -1);
			return;
		}
	}
	else{
		perm = IsPerm(theApp.m_pDlg->m_tiDIMPMount);
		if(!perm){
			_stprintf_s(this->m_msg, _T("DIMP_MOUNT tag is not permissive for set."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		retSt = VotedTagWrite(TWN_DIMP_MOUNT, 1, 0, DIG_VALUE);
		if(retSt != RT_NORMAL){
			_stprintf_s(this->m_msg, _T("Error writing DIMP_MOUNT tag value."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
		else{
			_stprintf_s(this->m_msg, _T("DIMP mast mounted."));
			this->InsertMessage(MSG_OK, this->m_msg);
			if(theApp.m_pDlg->m_pPosInfoDialog != NULL)
				theApp.m_pDlg->m_pPosInfoDialog->SetInputCoord(-1, -1);
			return;
		}
	}	
}

void ManualMode::OnSelchangeCmbFalv(){
	RETSTAT			retSt;

	this->m_cmbMMDispl.UpdateData(TRUE);

	if((!_tcscmp(this->m_FalvPos, _T("UPPER"))) && (this->m_cmbFALVL_sp.GetCurSel() == CMB_MM_FALVL_UP)){
		_stprintf_s(this->m_msg, _T("FALVL is already in upper position."));
		this->InsertMessage(MSG_WRN, this->m_msg);
		return;
	}
	if((!_tcscmp(this->m_FalvPos, _T("LOWER"))) && (this->m_cmbFALVL_sp.GetCurSel() == CMB_MM_FALVL_DOWN)){
		_stprintf_s(this->m_msg, _T("FALVL is already in lower position."));
		this->InsertMessage(MSG_WRN, this->m_msg);
		return;
	}
			
	if(this->m_cmbFALVL_sp.GetCurSel() == CMB_MM_FALVL_UP){
		if(IsPerm(theApp.m_pDlg->m_tiFALVL_up_req)){
			retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write FALVL UP request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 0, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot reset FALVL DOWN request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
		}
		else{
			_stprintf_s(this->m_msg, _T("FALVL_UP_REQ tag is currently not permissive for set."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
	}
	else if(this->m_cmbFALVL_sp.GetCurSel() == CMB_MM_FALVL_DOWN){
		if(IsPerm(theApp.m_pDlg->m_tiFALVL_dwn_req)){
			retSt = VotedTagWrite(TWN_FALVL_DOWN_REQ, 1, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot write FALVL DOWN request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
			retSt = VotedTagWrite(TWN_FALVL_UP_REQ, 0, 0, DIG_VALUE);
			if(retSt != RT_NORMAL){
				_stprintf_s(this->m_msg, _T("Cannot reset FALVL UP request value."));
				this->InsertMessage(MSG_ERR, this->m_msg);
				return;
			}
		}
		else{
			_stprintf_s(this->m_msg, _T("FALVL_DOWN_REQ tag is currently not permissive for set."));
			this->InsertMessage(MSG_ERR, this->m_msg);
			return;
		}
	}
	_stprintf_s(this->m_msg, _T("FALVL set point successfully updated."));
	this->InsertMessage(MSG_OK, this->m_msg);	
	
}

